Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes public class filename???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public class filename????" Watch "public class filename????" New topic

public class filename????

Mark Mokris
Ranch Hand

Joined: May 08, 2002
Posts: 61
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.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286

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.

GenRocket - A Test Data Generation Platform
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9043
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.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: public class filename????
Similar Threads
Source File declaration rule in java
why should the class name and the notepad name should be same?
declaring variables
Simple question on Applet
Naming a java file?