0

Possible Duplicate:
Can I compile a java file with a different name than the class?

Why must a file contain only a single, same-named Java class? Is there any way around this limitation?

Community
  • 1
  • 1
Aravinth
  • 27
  • 1

4 Answers4

1

Only name of public classes must be equivalent to the file name. And why do you want to break this?

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
  • It is sometimes helpful to have multiple classes which derive from a common supertype and differ only in how they invoke a protected supertype constructor, but which are not mutually interchangeable. For example, a `Motorcycle` and `Car` might both derive from a common `MotorVehicle` abstract class and differ only in the `WheelCount` value passed to the protected supertype constructor. Having them as separate types would allow a method which needs a `MotorCycle` to demand such a thing at compile time, but maintenance would be easier if the types were all defined together. – supercat May 24 '13 at 17:28
1

That's because the normal ClassLoader (URLClassLoader) uses the class (and package) name to find and load a class from the filesystem, a JAR file or a webserver (in the case of applets or Java WebStart).

In order to get around this, you would have to implement your own ClassLoader (not particularly difficult), and if you also want class names to be independant from source files, you're have implement/modify the compiler (probably a lot more difficult).

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

Did you... try searching google? or at least stackoverflow? It's just a convention, and afaik it only reffers to the filename being the same as the sole public class in your file. For java conventions this is a good source :)

pocorschi
  • 3,605
  • 5
  • 26
  • 35
-7

Java is just a broken language, you just have to accept that file name and path names have to match the class names.

No -- you do not want to break it.

Soren
  • 14,402
  • 4
  • 41
  • 67
  • Java can be many things, but it is not a broken language... Nothing bad in class name matching the filename. It makes your life easier... Every language has its own special things. – Petar Minchev Jul 30 '11 at 07:18
  • Common, Java is not really much better than fortran..... – Soren Jul 30 '11 at 07:20
  • @Petar I would say there are many reasonable arguments that Java sucks (although "broken" is not a good description), but this is one of the worst. – Karl Knechtel Jul 30 '11 at 07:21
  • I can say the same for C++ for example... `There are only two kinds of languages: the ones people complain about and the ones nobody uses` - Bjarne Stroustrup. – Petar Minchev Jul 30 '11 at 07:24
  • 1
    @Soren - have you ever written a non-trivial amount of FORTRAN? If you had, you'd know you were talking nonsense. (I have ...) – Stephen C Jul 30 '11 at 07:43
  • @Stephen -- yes I have --- not sure why that is relevant – Soren Jul 30 '11 at 07:47
  • @Soren - what's not relevant? The fact that you know you are talking nonsense is highly relevant ... IMO. – Stephen C Jul 31 '11 at 01:14