简体   繁体   中英

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 ?

I don't believe that it is possible to have a true nested class with two classes in separate files.

The closest thing that I think you can get is by declaring the fields in the "inner" class that you'd like to access as protected rather than private . That way you can access them from the "outer" class as long as they are in the same package. Note that this would mean other classes in the package would have access to these fields as well.

If it is important that only the outer class can access the fields, I would suggest that you just nest the inner class.

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.

One way is to move your classes into their own package, where the Outer class declares all its members that it wants to share with the Inner class to be package private (no access modifier).

package some.pkg;

public class Outer{
    int sharedMember;

    // Other field, consturctors methods and so on
}

class Inner extends Outer{
    void doSomething(){
         int i = super.sharedMember;

         // Do something with it
    }
}

This is what many classes in the java sources already do. You still have to extend to Outer class with the Inner though for this to work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM