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?
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.
Yes, using cascade-jumper. A cascade-jumper is a abstract and non-static inner class that is defined anywhere else.
MyClass.java
MyNestedClass
Regards
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.
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).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
orjava.util
.in pkg/MyClass.java
in pkg/MyHiddenClass.java
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.i think you have a god object or something like that, think in refactor your code
http://en.wikipedia.org/wiki/God_object