Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem compiling  RSS feed

 
james hargis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I am followin the code in head first java and am running into a problem every time i try to compile. I get this error:

C:\java>javac beer.java
beer.java:1: class BeerSong is public, should be declared in a file named BeerSo
ng.java
public class BeerSong {
^
1 error

If I remove the public from the beginning of the line then everything compiles and works fine. Why is this?

Thanks
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Thank you for posting the whole message, which tells you exactly what is wrong. The problme is not that BeerSong is public, the problem is that it isn't in a file called BeerSong.java. Yo can't have more than one public class per file, and the name of the file and the name of the public class have to be the same. Move the BeerSong class into its own file.

And don't worry about the compiler not compiling BeerSong; when it finds a declaration of a BeerSong class in your code, it will go and look for the BeerSong.java file.
 
james hargis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh ok I see... its because I didnt name the file BeerSong.java. I just named it beer.java. I get it. thanks for the quick reply. I am really new to programing. I have learned a little bit of C++ but not much. I am taking a java class this summer at the local CC and this book looks like it is going to help a lot. I just finished chapter 1 and seem to be getting it so far.

Thanks again
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only too happy to be able to help.
[ June 18, 2006: Message edited by: Campbell Ritchie ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!