Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, can we divide a class into multiple files

Tags:

java

class

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

like image 594
user496949 Avatar asked Mar 15 '11 08:03

user496949


People also ask

Can we create separate classes as separate files in Java?

Of course you can separate out 2 classes and still they will work fine. Just make sure you import one class in another using import statement. All you need to do is move AnotherClass class into a separate source file named with name same as class name ie "AnotherClass.

Should Java classes be in separate files?

It is technically legal to have multiple Java top level classes in one file. However this is considered to be bad practice (in most cases), and some Java tools may not work if you do this.

Can Java have multiple classes in one file?

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.

Should classes be in different files?

If you have a class that really needs to be a separate class but is also only used in one place, it's probably best to keep it in the same file. If this is happening frequently, though, you might have a bigger problem on your hands.


4 Answers

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 :)

like image 81
Jon Skeet Avatar answered Oct 17 '22 06:10

Jon Skeet


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.

like image 44
Graf von und zu Nix Avatar answered Oct 17 '22 06:10

Graf von und zu Nix


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.

like image 11
spa Avatar answered Oct 17 '22 08:10

spa


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.

like image 5
sleske Avatar answered Oct 17 '22 07:10

sleske