48

I have a class MyClass and an inner class MyNestedClass like this:

public class MyClass {
  ...
  public class MyNestedClass {
    ...
  }
}

Both classes are very long. Because of that i'd like to seperate them in two different files, without breaking the hierarchy. This is because the nested class shouldn't be visible to the programmer who uses MyClass.

Is there a way to achieve that?

Alp
  • 29,274
  • 27
  • 120
  • 198
  • similar question http://stackoverflow.com/questions/2764234/partial-classes-partial-class-file – Terrance Jun 02 '11 at 18:42
  • I had a similar problem and solved it here: http://stackoverflow.com/q/27284667/1190665 At least that's the best I came up with. – John Dec 04 '14 at 01:34

6 Answers6

20

You can make the inner class package private which means that it will only be accessible from other classes in exactly the same package. This is also done quite frequently for hidden classes inside the standard JDK packages like java.lang or java.util.

in pkg/MyClass.java

public class MyClass {
  ...
}

in pkg/MyHiddenClass.java

class MyHiddenClass {

  final MyClass outer;

  MyHiddenClass( MyClass outer )
  {
      this.outer = outer;
  }
  ...
}

Now when you want to access methods or variables of the outer class you need to prefix them with outer. but you get essentially the same functionality as before when the reference to the outer instance was synthetically created by the compiler.

x4u
  • 13,877
  • 6
  • 48
  • 58
8

No. Java source codes can not be split across multiple files. You'd need a construct similar to a partial class as in C#, which Java does not have.

retrodrone
  • 5,850
  • 9
  • 39
  • 65
7

i think you have a god object or something like that, think in refactor your code

http://en.wikipedia.org/wiki/God_object

osdamv
  • 3,493
  • 2
  • 21
  • 33
5

Objects of inner classes keep implicit references to the objects of the paren class. If the nested class is not static (it is inner) you can not. But if the nested class does not need access to the parent's class instances directly and does not need access to the private fields, than should be ok to refactor, move the inner class out and do not declare it public (out of that package can not be accessed).

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
4

Yes, using cascade-jumper. A cascade-jumper is a abstract and non-static inner class that is defined anywhere else.

MyClass.java

public class MyClass {
  public abstract class CascadeJumper {}
}

MyNestedClass

private class MyNestedClass extends MyClass.CascadeJumper {
  MyNestedClass(MyClass dollarOne) {
    dollarOne.super(); // yes, is possible!
  }
}

Regards

Grim
  • 1,938
  • 10
  • 56
  • 123
  • I don't see how this could work. From `MyNestedClass` you will still only be able to access public methods and fields of `MyClass`, as from any other class. – Alphaaa Nov 22 '16 at 10:07
  • @Alphaaa Nope, you can access private fields, private enums, private classes and private methods, private constructors, simply everything from `MyClass` in `MyNestedClass`! Try it! – Grim Nov 22 '16 at 18:02
  • I did and you can't. In fact even normal inner classes can't according to this: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_13.htm. The java compiler actually converts accesses to the outer class' private fields by generating package private methods and substitutes any direct field access with a call to one of these generated methods. All classes are in effect top level. – user2219808 Feb 26 '17 at 13:59
  • This doesn't work, though it looks cool. Private attributes means that they are only accessible from within the class. It doesn't work when my nested class is external. This may only work if I reference the necessary attributes for calculation of the outer class as protected attributes of the abstract nested super class. – Quan To Mar 09 '17 at 03:37
1

You could create abstract base classes that you extend in both.

However, if those classes are large, you might be better off re-evaluating your class design and see if you can refactor into classes which represent one concern each.

rsp
  • 23,135
  • 6
  • 55
  • 69