Good day. Recently, I began to learn Java from the book "Learning Java" and there was a problem with classes of more than two in one file.

Reached the objects, but somehow they do not fit. The book provides sample code, I enter it into NetBeans, but the program refuses to run. Yes, at the beginning of the book it was said not to use IDE, but still help, what can you, please, I am still very green.

Here is the error: error What just did not try to do, I can not fix it.

Here is the code:

class Movie { String title; String genre; int rating; void playIt() { } } public class MovieTestDrive { public static void main(String[] args){ Movie one = new Movie(); one.title = "Как Прогореть на Акциях"; one.genre = "Трагедия"; one.rating = -2; Movie two = new Movie(); two.title = "Потерянные в Офисе"; two.genre = "Комедия"; two.rating = 5; two.playIt(); Movie three = new Movie(); three.title = "Байт-Клуб"; three.genre = "Трагедия, но в целом веселая"; three.rating = 127; } } 
  • And this "what just tried" did not include the placement of this code in the MovieTestDrive.java file? Or deleting the word public in the class declaration? - Regent
  • and does not start, but does not compile - gil9red
  • I tried and compiled the program (as gil9red noted), but nothing is done. I did not remember whether I deleted the public, but it seemed to be. - Kasbotov Idar

1 answer 1

in 1 file there can be only 1 "main" class, the name must match the file name. There may be several classes inside a class.

MovieTestDrive must be inside Movie

 class Movie { String title; String genre; int rating; void playIt() { } public static class MovieTestDrive { public static void main(String[] args) { Movie one = new Movie(); one.title = "Как Прогореть на Акциях"; one.genre = "Трагедия"; one.rating = -2; Movie two = new Movie(); two.title = "Потерянные в Офисе"; two.genre = "Комедия"; two.rating = 5; two.playIt(); Movie three = new Movie(); three.title = "Байт-Клуб"; three.genre = "Трагедия, но в целом веселая"; three.rating = 127; } } } 
  • Thanks, I will try - Kasbotov Idar
  • By the way, on account of this. I didn't quite figure out how to place a class inside a class. Do not think for arrogance, but could you explain it to me. Just really want to understand. Did you just move the curly bracket closing first class to the end of the code? - Kasbotov Idar
  • one
    In one file it can be how many classes are acceptable, but only one public one. And its name must match the file name - Anton Shchyrov
  • @AntonShchyrov and what do you ultimately mean by "only one public"? The inner class may well have a public modifier and be accessible from other packages. Are you still about the option in which there are two external classes in one file? - Regent
  • Now when I compile, a window opens with the inscription: "The movie.Movie class was not found in the movie project" :( - Kasbotov Idar