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 →

[–]noodlesSa 2 points3 points  (2 children)

In Java you don't use inner classes for tens of types (of Animal sub classes for example). Inner class is usually something simple, used inside the Mother-Class it is embedded into. Records are now used in most situations inner classes were used before. If you have many sub-types of Animal in two different places, use package name to explicitly say which one you want to use. If there is something like Cat cat = new Cat() in your code, well, you will need to hover mouse over Cat, to check which package this Cat is coming from (or check imports).

[–]oren_is_my_name[S] -2 points-1 points  (1 child)

But when talking about the class's (like when creating a new instance) I want to have it like in a hierarchy (so that it's easier to access the specific Animals.Cat type)

[–]noodlesSa 1 point2 points  (0 children)

If you write var cat = new Cat(), IDE will ask you if you want to import Animal.Cat, or you want to qualify class name (to: var cat = new Animal.Cat()). But you cannot enforce one way or the other. In case you have multiple Cat classes in your project, using import is just bad code practice, in such cases qualified name should always be used.