jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Very new to java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Very new to java" Watch "Very new to java" New topic

Very new to java

Amy Salerno

Joined: Jun 16, 2007
Posts: 3
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

Joined: Sep 11, 2006
Posts: 959

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.

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
lei feng

Joined: Aug 23, 2007
Posts: 26
you can remove "public" key words
marc weber

Joined: Aug 31, 2004
Posts: 11343

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.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Amy Salerno

Joined: Jun 16, 2007
Posts: 3
I removed the extra public word. Thank you all for your replies
I agree. Here's the link: http://aspose.com/file-tools
subject: Very new to java
It's not a secret anymore!