• 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

problem compiling

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only too happy to be able to help.
[ June 18, 2006: Message edited by: Campbell Ritchie ]
reply
    Bookmark Topic Watch Topic
  • New Topic