Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java pg. 37-38

 
George Koenig
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Head First Java as a learning tutorial and the example on pg. 37-38 does not compile. Returns a "class Player is public, should be declared in file named Player.java. Same for GameLauncher. I tired to compile them separatly but then I get other errors. The book's web site doesn't have a contact page so I'm trying here as some of the contributors also contribute to this site. My apologies if this posting is inappropriate as I realize it's meaningless without seeing the book.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well.. most of us don't have that book (yet), so it'd be great if you explain more the context of the problem, and if you can add some code snippets we will all try to help you.
 
Joe Pluta
Ranch Hand
Posts: 1376
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The very first thing is to make sure that the code for the Player class is in a file called Player.java. Case is important - only the "P" should be capitalized, the rest of the letters should be lower case.
Joe
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remove the word 'public ' from the front of 'public class Player' and from 'public class GameLauncher' and it compiles/runs OK.
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George;
I typed in code word for word as it is in the book - everything compiles and works as advertised - as is. My guess is as Joe suggested: make sure the case of the file you saved is the same of the name of the class in your code.
 
George Koenig
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed 'public' from the 3 classes and compiled using GameLauncher.java (I had been using GuessGame.java) and it worked.
I'd like to ask doco if he typed all three classes into one file and saved it as GameLauncher.java as that's what I tried and still got the error - class public... needs own file when I tried to compile.
Thanks to all who wrote to help, I'm sure I'll be back for more.
-george
 
George Koenig
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally got it to work as written. Each 'public' class is in it's own classname.java file and the classname.java file which contains 'main' is the only one you compile with javac the others automatically get compiled.
Thanks again -george
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic