17

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?

4
  • 5
    In Java, it is customary to name all class names with a capital letter and all variable names with a lowercase letter. You should call your classes MySimpleClass and MyNotSoSimpleClass. Commented Nov 10, 2010 at 14:21
  • 1
    Why do you have a class that does nothing except wrap an array, which is also a class? Commented Nov 10, 2010 at 15:36
  • @JeremyP: It's an example. My actual simple class has a few variables, and my not so simple class has much more code, including functions. Commented Nov 10, 2010 at 15:58
  • +1 However, the title asks for reasons in design decisions, why the question body asks for a way to do what you want. You could amplify the title to more accurately reflect what the question is about. Commented Oct 24, 2013 at 11:10

9 Answers 9

17

You can have multiple classes in the same file, but only one of them can be public:

Java: Multiple class declarations in one file

Sign up to request clarification or add additional context in comments.

6 Comments

You can have public inner classes, what are you trying to say?
He's not referring to inner classes at all, but additional top level classes.
"Only one of them can be public" is incorrect.
@Andy: No, "Only one of them can be public" is perfectly correct. Further I can't understand why this was downvoted.
@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/… .
|
15

It is possible to have one class inside of another class. In Java, it's called an inner class

4 Comments

What would be the custom here? Using an inner class for this kind of helper class or one of those package private classes?
This can be a good way to go, but be sure you won't be needing this class elsewhere first.
Inner classes are one type of nested class. There are also static nested classes.
I'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.
13

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.

6 Comments

The 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.
I 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.
@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.
+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".
On Windows, there are nice grep-like tools, for example code.google.com/p/dngrep :)
|
7

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

4 Comments

Incorrect. Nested classes can be public.
Ah, my apologies. Forgot about that case. I'll make an edit to my answer.
Second paragraph is still incorrect. Fix that and I'll remove my downvote.
I've updated it. If I've missed something please do tell though. I'm here to learn too.
4

The rule is one top-level public class per file. Inner class, private class and some other stuff are allowed

4 Comments

-1 Incorrect. The limitation is on top-level classes only.
@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.
You may presume, because you understand how it works. Someone who is here trying to understand cannot make the same presumption.
I added the word "top-level" and replaced my downvote with an upvote.
3

Java allows you to create inner classes. That is, classes within another class. That's where you should put your helper classes. On the other hand, I feel your pain. Being primarily a C++ coder myself Java can sometimes feel like it forces boilerplate upon you.

Comments

3

Yes, you can nest a class inside another.

Contrary to many of the answers here, there are no restrictions on public visibility.

Nested classes include:

  • Static nested classes.
  • Inner classes, which have an implicit pointer to an instance of the containing class, and are allowed to access its private parts.
  • Anonymous inner classes, which can be created within methods of the containing class, without a name. These are convenient, for example, for short event listeners.

More details are available in this Oracle tutorial.

Comments

2

In Java, there is no way to put two public top-level classes into the same file, but this restriction does not apply to private/inner classes. So you could have something like:

public class MyNotSoSimpleClass { private MySimpleClass mySimpleClass = new MySimpleClass(); private class MySimpleClass { public float numbers[4]; ... } .... 

}

5 Comments

This 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.
Obviously. Thus private/inner classes. I'm not saying they are the same thing. Inner classes are, by definition not top level...
@Andy: Obviously, user468341 was talking about top-level classes.
@user458341 - "Public" is not a synonym for "top-level." Nested classes are not required to be private.
This 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.
0

In Java there is something called nested classes, read more about it here!

1 Comment

-1 Poor format for an answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.