• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about classes and running source code.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will find them as long as they are in your CLASSPATH.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I earlier wrote: . . . leave out the package names . . .

The link Christophe gave you calls that using the "unnamed package".
 
reply
    Bookmark Topic Watch Topic
  • New Topic