David Thielen
David Thielen

Reputation: 32926

Is there a way to put nested classes in a separate file?

I have a Java class that is about 4,000 lines long (lots of methods). This class then uses about 200 small classes that only it needs, so another 4,000 lines of code.

If this was C# I would put those other in a partial class file (different file), but they would remain private nested classes only visible to the parent class.

Is there a way to do this in Java? I'm not asking for some methods to be in a distinct file, but for private nested classes to be in a distinct file.

Upvotes: 5

Views: 3581

Answers (4)

Mahozad
Mahozad

Reputation: 24532

Here is a way to do it using JUnit 5 (in Kotlin, but with a little change should be fine for Java too).

MyContainerTest.kt (or .java) file:

class MyContainerTest {
    @Nested inner class MyNestedTestsOne : MyNestedTest1()

    @Nested inner class MyNestedTestsTwo : MyNestedTest2()

    // Can also have regular unit tests
    @Test
    fun myUnitTestInContainer() { /* ... */ }

    @Test
    fun myOtherUnitTestInContainer() { /* ... */ }
}

MyNestedTest1.kt (or .java) file:

abstract class MyNestedTest1 {
    @Test
    fun myUnitTestInNestedClass1() { /* ... */ }
}

MyNestedTest2.kt (or .java) file:

abstract class MyNestedTest2 {
    @Test
    fun myUnitTestInNestedClass2() { /* ... */ }
}

Nested test classes have been extracted to separate files and then in the desired container class, placeholder nested classes have been created which inherit from those. This way, they are shown as the sub-hierarchy of the container test class in test views and reports.

Note: Make those nested classes abstract to avoid running them twice.

See https://github.com/junit-team/junit5/issues/1750

Upvotes: 0

Ralf Kleberhoff
Ralf Kleberhoff

Reputation: 7290

You can replace the inner classes with top-level ones, but you'll have to rewrite a lot of things by hand that the compiler auto-wires for you with the inner-class relationship. To the Virtual Machine, an inner class is nothing special, it's just another class in the same package as the outer class with a fancy name. But the compiler creates a lot of helper constructs under the hood, that you have to reconstruct by hand (or have some refactoring tool do that for you):

  • The inner class can refer to the outer this instance, by prefixing it with the outer class name. You need to pass the outer this into your inner constructor and store it in a field like outerThis to get access.
  • In the source code, you can call the outer-class methods directly. You need to rewrite it like outerThis.method(). The same applies to fields.
  • For private outer methods and fields to become accessible, the compiler creates bridge constructs for you. You have to either change access modifiers or create package-private bridge methods yourself.

In the end, you'll have the former inner classes at least package-visible and being more verbose than the original ones, but on the other hand you'll get better isolation and testability.

Upvotes: 0

Bernhard Barker
Bernhard Barker

Reputation: 55609

You can't make a class private to only another class while putting it in a different file.

Use no class access modifier

What you can do is put the classes in separate files with no access modifiers (omit "public"), which will make them package-private, i.e. visible only within its own package. See also the official Access Control tutorial.

UtilClasses.java:

package OurPackage;

class UtilClass1
{
}
class UtilClass2
{
}

MainClass.java:

package OurPackage;

public class MainClass
{
   UtilClass1 iAmAUtilClass;
}

Use interfaces or inheritance

You can also achieve something similar with either interfaces or inheritance, by omitting the access modifier from the nested class. This would also be package-private, but this might be preferable to the above in some circumstances, since it avoids having all the nested classes at the top level.

BaseInterface.java:

package OurPackage;

interface BaseInterface
{
   class UtilClass1
   {
   }
}

MainClass.java:

package OurPackage;

public class MainClass implements BaseInterface
{
   UtilClass1 iAmAUtilClass;
}

You can also use a base class instead of an interface and extend that with roughly the same effect.

You don't need to implement BaseInterface gain access to its nested classes, but, if you don't, you'd need to use BaseClass.UtilClass1 instead of just UtilClass1.

Upvotes: 3

Lothar
Lothar

Reputation: 5449

Inner private classes can't be "extracted" and still be visible only to one particular class. One solution is already mentioned in the comments: Create a package that contains the "main" class and all the previously inner classes and make the inner classes package visible. This would also allow you to create unit tests testing for the correct functionalities of the inner classes, which is something that is most likely currently not happening simply because the inner classes can't be "reached" by a unit test at the moment.

Concepts like declaring "friendships" between classes like in C++ don't exist in Java.

Upvotes: 1

Related Questions