Why only one class per file
Solution 1
It is possible to have one class inside of another class. In Java, it's called an inner class
Solution 2
You can have multiple classes in the same file, but only one of them can be public
:
Java: Multiple class declarations in one file
Solution 3
As several others have pointed out, you can have any number of non-public classes in a single file.
To answer the "why", one likely factor is that it makes your classes easy to find. If I see a reference in a program to class Foobar, I immediately know that it must either be in the same file with the current class, or it must be in a file named Foobar.java. I can check the imports to see what directory it is in.
When I work on C or C++ programs, I regularly find myself having to do Windows file searches or Linux grep's to find the source for a class.
When I write very small programs, I often put all the classes in one file. But if it's going to be more than a few thousand lines, it's much more manageable to break it out.
When I have a class that is only used by one other class, I'll often put it in the same file. Usually this means a class that just exists to temporarily hold some small bundle of data, like I need an array or list of something that requires more than one field.
Solution 4
The restriction is that you can only have one top level public class per file (you can have nested public classes though). As people have mentioned you can have inner/nested classes within that class.
Another thing which hasn't been mentioned is that you can have another top level class in the same file, as long as it is package-private (visible only within the package). A package-private class is one which is declared without an access modifier.
Here's a chapter from Effective Java (which is worth a read) on this subject. http://books.google.co.uk/books?id=ka2VUBqHiWkC&lpg=PA67&ots=yXKoLnv2TX&dq=Minimize%20the%20accessibility%20of%20classes%20and%20members&pg=PA67#v=onepage&q&f=false
Solution 5
The rule is one top-level public class per file. Inner class, private class and some other stuff are allowed

Espen
Updated on September 20, 2022Comments
-
Espen 3 months
Still coming from C++ I find it cumbersome to create many small helper classes and objects. Mostly because I have to have one file per class. I basically have a class like this:
public class mySimpleClass { public float[] numbers = new float[ 4 ]; }
And then I have this class:
public class myNotSoSimpleClass { private mySimpleClass; . . . }
The second class which is not so simple is ok to have in its own file. However, the simple class is connected to the not so simple class and it would be very nice to not have to have those few lines of code in its own file.
So to sum it up, this is what one could do in C++:
public class myNotSoSimpleClass { private struct mySimpleClass { float numbers[ 4 ]; } myStruct; . . . }
Is it possible to embed/use one class inside another class, or the same file? I would just find it easier to work with large projects if I could set up these two classes into one file. Or is Java a strictly one class per file, and that's it, language?
-
Buhake Sindi about 12 yearsYou can have public inner classes, what are you trying to say?
-
Sean Reilly about 12 yearsHe's not referring to inner classes at all, but additional top level classes.
-
Andy Thomas about 12 yearsThis is false. It is possible to have arbitrarily many public classes in the same source file. It's just not possible to have more than one top-level class in the same file.
-
Erick Robertson about 12 years-1 Poor format for an answer.
-
Erick Robertson about 12 years-1 Incorrect. The limitation is on top-level classes only.
-
Andy Thomas about 12 years"Only one of them can be public" is incorrect.
-
Espen about 12 yearsWhat would be the custom here? Using an inner class for this kind of helper class or one of those package private classes?
-
nosirrahcd about 12 yearsObviously. Thus private/inner classes. I'm not saying they are the same thing. Inner classes are, by definition not top level...
-
chiccodoro about 12 years@Andy: No, "Only one of them can be public" is perfectly correct. Further I can't understand why this was downvoted.
-
jon_darkstar about 12 yearsThis can be a good way to go, but be sure you won't be needing this class elsewhere first.
-
Andy Thomas about 12 yearsInner classes are one type of nested class. There are also static nested classes.
-
chiccodoro about 12 years@Andy: Obviously, user468341 was talking about top-level classes.
-
Jay about 12 years@Erick: How does that make his answer incorrect? He said "private class", by which I presume he means a top-level class that is not public.
-
Andy Thomas about 12 years@chicodoro, you may be thinking of the restriction that at most one may be a top-level class. Here's an example of a public class nested in another public class: download.oracle.com/javase/6/docs/api/java/nio/channels/… .
-
Dave McClelland about 12 yearsI'm not sure there's any proper "custom" for how to solve it. I prefer separate files, but I learned Java before C++ and it's a personal preference.. If you don't need it outside the class, make it private. If you do need it outside the class, make it protected. It all comes down to what your situation dictates.
-
Andy Thomas about 12 years@user458341 - "Public" is not a synonym for "top-level." Nested classes are not required to be private.
-
Andy Thomas about 12 yearsIncorrect. Nested classes can be public.
-
Sinjo about 12 yearsAh, my apologies. Forgot about that case. I'll make an edit to my answer.
-
Kelly S. French about 12 yearsThe amount of time it takes to create a new file per class is tiny compared to the time savings of being able to find the code without searching. You don't have to look through several levels of #includes or try to manually parse the makefile to find which file has which class. +1 for the explanation.
-
Espen about 12 yearsI see the point of having one file for each class. However, I never had this much problem finding a class in C++. Mainly becasue I think I do proper naming and namespacing, and becasue of the nifty right-click menu in VS. Though looking through the std or external libraries would be a pain without it. When that is said, having one file each for ten or twenty three-liner classes would just be a hassle.
-
Andy Thomas about 12 yearsSecond paragraph is still incorrect. Fix that and I'll remove my downvote.
-
Erick Robertson about 12 yearsYou may presume, because you understand how it works. Someone who is here trying to understand cannot make the same presumption.
-
Erick Robertson about 12 yearsIt's an incomplete answer, and it doesn't say that it's incomplete. You can also have inner classes, which is actually what the OP posted in his C++ code. That makes this answer poor.
-
Erick Robertson about 12 yearsI added the word "top-level" and replaced my downvote with an upvote.
-
Sinjo about 12 yearsI've updated it. If I've missed something please do tell though. I'm here to learn too.
-
Jay about 12 years@Espen: Sure, it can seem silly to have to create a separate file just to hold one tiny class. But I go back to my original point: a consistent rule makes the desired source code easy to find. Modern IDEs can help, but I'm not always using an IDE to look at my code.
-
n611x007 about 9 years+1 (only) This answer actually provides reasons for why forcing what Java forces. It also gives practices! All these can put your "Java mind" in "the right context".
-
n611x007 about 9 yearsOn Windows, there are nice grep-like tools, for example code.google.com/p/dngrep :)
-
Jay about 9 yearsSure. It's not like a "random" assignment of classes to files would make the code impossible to manage. It just makes it harder. If this simple rule saves me a few minutes every day, that adds up.
-
pdem about 4 yearsThis answer is ambiguous, it talk about many top level classes, and the example gives a one top level class with a inner class. It should give the 2 examples.