• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about classes and running source code.  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started reading "Head First Java" and am on the GuessGame program shown below. When I try to compile it using the JDK I get an error for each class. "class GuessGame is public, should be declared a file named GuessGame.java", "class Player is public, should be declared in a file named Player.java", and "Class names, 'GameLauncher', are only accepted if annotation processing is explicitly requested". I have no idea what the 3rd error means but do the first two mean that I need to individually save each class as its own file? Or make the ones that aren't the one with the main() not public? I'm confused about what the file should be named before compiling it. Right now it's named GameLauncher.java because I thought that the name of the file had to be the same as the class with the main in it or something. This was because previously code wouldn't compile unless it was blank.java with the blank being the name of the main class. What exactly determines what the file should be named before compiling? Since everything in java has to be in a class, which class does the compiler look at first? I assume the code below is correct because it is the same that is presented in the book? Help would be greatly appreciated .




 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The filename is the name of the public class. You can only have one public class per file. Move other public classes to their respective file. If you want to keep them in the same file, you can remove the "public" keyword.
 
Brian Fenno
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried running it with only one public class and it worked. But then how would I get the program to run with 3 separate class files? Would they automatically work with each other?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler will find them as long as they are in your CLASSPATH.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

At this stage, leave out the package names, and put all the files in the same directory. Navigate to that directory, and the compiler will look for files with the same name as the classes. It will still find classes in other files in that directory. If you don't set a classpath, the compiler will look through the current directory.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote: . . . leave out the package names . . .
The link Christophe gave you calls that using the "unnamed package".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!