This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript 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: 55324
157
  • 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: 55324
157
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!