Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Very new to java

 
Amy Salerno
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am using Head First Java book.Using textedit and terminal on a mac. This will not compile Any help would be greatly appreciated. Thanks in advance
guessgame.java:1: class GuessGame is public, should be declared in a file named GuessGame.java
public class GuessGame {
^
guessgame.java:59: class Player is public, should be declared in a file named Player.java
public class Player {
^
guessgame.java:66: class GameLauncher is public, should be declared in a file named GameLauncher.java
public class GameLauncher {
^
3 errors
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One java file can only have one public class <ClassName>.
For your case, you basically you need 3 java files.
- GuessGame.java
- Player.java
- GameLauncher.java

So don't put everything into one file.
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can remove "public" key words
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch! (Always nice to see more Mac users. )

As already indicated, each .java file can contain at most one top-level class that's declared public. (It is not required to contain any.) If a .java file contains a top-level public file, then the name of the file must match that public class name exactly.

But also note that Java is case-sensitive, so "guessgame" (the name of your file) is not the same as "GuessGame" (the name of the class).

The best approach here is to put each top-level class in its own separate file, named to match the class name. This keeps things nice and neat, so you can easily find a class definition by the name of the file. Although as lei suggested, you could get around this by removing the "public" modifier from the class declarations.
 
Amy Salerno
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed the extra public word. Thank you all for your replies
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic