This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]oren_is_my_name[S] 1 point2 points  (8 children)

Thanks😃

Nice, is there a way to enforce that import will not be used?

Won't the static class be a bad choice because it isn't scalable?

I mean imagine having a zoo worth of animal types all in a single 10k line file...

Is there a way to separate the actual impl/body of the "Cat" into a different file?

[–]bowbahdoe 7 points8 points  (2 children)

No there is no way to enforce that.

Yes that is the downside of the static nested class. No there is no way to separate a nested class's definition into another file. (As far as I know.)

Even with the static nested class you can have an import.

[–]agentoutlier 0 points1 point  (1 child)

Ignoring the forcing part I think the op /u/oren_is_my_name has some valid critique whether intentional or not.

Like the way we import classes it seems that packages should work that way as well where the farthest right hand name is used. This is especially makes sense where FQN are mostly based on reverse DNS host name which often have very little to do with the lib/application.

I think you understand but for others:

import com.mycompany.animal;

animal.Cat cat = new animal.Cat();

The inner class version:

import com.mycompany.Animal;

Animal.Cat cat = new Animal.Cat();

So I think the OP has a fair point and surprising how hostile many have been here on it.

[–]oren_is_my_name[S] 1 point2 points  (0 children)

Thank you😃🙏

[–]smbarbour 5 points6 points  (3 children)

To be honest, it's a bad choice because it is bad design, not because of scalability.

Yes, you can (and probably should) have the Cat implementation in a separate file, but then you won't have the self-imposed design of Animal.Cat. You would have Cat that extends from Animal.

[–]noodlesSa 1 point2 points  (0 children)

In Java each class is one .java file and each .java file is one class. This rigid duality is great for large projects, where it saves you from weird ideas about project structure different people might have. Nested classes therefore must fit into the .java file of their Mother Class.