• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public class filename????

 
Mark Mokris
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working through several examples in a Java book. Why does the name of my program file have to match the name of my public class. I have other file with class that don't match the file name.
Thanks!
Mark
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, for one, that is the way the java compiler works. And another reason is this is just good design. It makes sense that your .class file really be the name of your CLASS.
What do you mean you have other files where the class names don't match the file name?
I would suspect 2 things.
1. They won't compile
2. You are talking about inner classes. So you would still have your main Class that would be the same name as your File.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A public class must be in a file of the same name. You can put other classes in the same file if they are not public.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic