• 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

Head First Java pg. 37-38

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
buckaroo
Posts: 401
Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic