In Java, can we divide a class into multiple files

JavaClass

Java Problem Overview


Any possibility to divide a class into multiple physical files using Java?

Java Solutions


Solution 1 - Java

No, the whole of a class has to be in a single file in Java.

If you're thinking of C#'s "partial types" feature, there's no equivalent in Java. (If you weren't thinking of C#, ignore this :)

Solution 2 - Java

Yes You Can!

For the sake of completion:

Since Java 8, you have the concept of default methods.

you can split up your class into multiple files/subclasses by gently abusing interfaces

observe:

MyClassPartA.java
interface MyClassPartA{
    public default int myMethodA(){return 1;}
}
MyClassPartB.java
interface MyClassPartB{
    public default String myMethodB(){return "B";}
}

and combine them:

MyClass.java
public class MyClass implements MyClassPartA, MyClassPartB{}

and use them:

MyClass myClass = new MyClass();

System.out.println(myClass.myMethodA());
System.out.println(myClass.myMethodB());

You can even pass variables between classes/files with abstract getters and setters that you will need to realize/override in the main class, or a superclass of that however.

Solution 3 - Java

This might be a good idea if the class is really so large such that the implemented concepts are not easy to grasp. I see two different ways to do this:

  1. Use inheritance: Move general concepts of the class to a base class and derive a specialized class from it.

  2. Use aggregation: Move parts of your class to a separate class and establish a relationship to the second class using a reference.

As previously mentioned, there is no concept like partial classes in Java, so you really have to use these OOP mechanisms.

Solution 4 - Java

Using just javac, this is not possible. You could of course combine multiple files into a single .java file as part of your build process, and invoke javac afterwards, but that would be cumbersome on so many levels that it is unlikely to be useful.

Maybe you could explain your problem, then we can help better.

If you feel your .java files are too large, you should probably consider refactoring.

Solution 5 - Java

Of course it is possible, but I don't think it's useful at all.

To start off, divide isn't really the question I guess, you just compile the file and split it up whichever way you want.

Now to put them back together all you need to do is to write a custom class loader which loads all the pieces, combines them into a single byte array, then calls defineClass().

Like I said, it does look pretty pointless and is probably not what you want and definitely not what you need, but it is technically possible.

(I did something similar once as a joking way of obfuscating code: bytes of the class file were scattered in constants of all the other classes in the application. It was fun, I have to admit.)

Solution 6 - Java

No, in Java this can not be done.

Solution 7 - Java

No you can't. If your class is too big than you should split it into two or more.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser496949View Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaGraf von und zu NixView Answer on Stackoverflow
Solution 3 - JavaspaView Answer on Stackoverflow
Solution 4 - JavasleskeView Answer on Stackoverflow
Solution 5 - JavabiziclopView Answer on Stackoverflow
Solution 6 - JavaPeter KnegoView Answer on Stackoverflow
Solution 7 - JavabyyykView Answer on Stackoverflow