Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Nested classes in other files

I have something similar to this:

class Outer {
  // a lot of code...
  class Inner {
    // a lot of code...
  }
}

This is exactly what I want in a technical way (I DO want Inner to be a nested non-static class). The file is very long and I use patterns like this often in this project. To improve readability, I'd like to put the Outer class and the Inner class in their own respective files -- but I they should remain as 'Inner' and 'Outer' (that is, the Inner class as access to an object of the Outer class and can access any fields including the private ones).

One solution I thought of would be to put a nested interface and inherit it in another file. Based on this question however, it appears that nested interfaces can only be static.

Also note that this question IS NOT a duplicate of this one: "putting nested classes in separate files", as that question had the intent to make a nested class un-nested. I want the inner class to still be an inner class, just, in another file.

This question is about abstract inner classes, but it is not specified whether a child of that abstract class would also be considered inner or not by the JVM.

Is this possible ? If so, how can I do it ?

like image 601
CLOVIS Avatar asked Jun 21 '18 16:06

CLOVIS


People also ask

Can classes be nested within each other?

The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here: class OuterClass { ... class NestedClass { ... } }

Can an inner class be accessed from outside package?

Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.

Are nested classes inherited?

A nested class may inherit from private members of its enclosing class. The following example demonstrates this: class A { private: class B { }; B *z; class C : private B { private: B y; // A::B y2; C *x; // A::C *x2; }; }; The nested class A::C inherits from A::B .

Is it good practice to use nested classes?

Nested Class can be used whenever you want to create more than once instance of the class or whenever you want to make that type more available. Nested Class increases the encapsulations as well as it will lead to more readable and maintainable code.


1 Answers

As far as I'm aware there's no way to have an inner class in a separate file and have it keep its privileged position. Java doesn't have an #include type syntax. Java expects a single class to "own" a .java file, which in this case is your outer class. You can't directly access internal privates or locals from your outer class from any other .java file.

Your best bet is probably to move parts of your inner class to its own class, make it abstract, and move as much of the logic as you can into it. Create a new inner class and have it extend the new stand-alone class and have the inner class implement anything that requires access to the internals of the outer class which you won't be able to access externally.

like image 57
Michael Powers Avatar answered Sep 29 '22 16:09

Michael Powers