53

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

Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 3
    +1, I thought it is there, I'm into C# now. – Sanjeevakumar Hiremath Mar 15 '11 at 08:43
  • do you mean splitting a big Java file to small modularized classes? – asgs Mar 15 '11 at 08:43
  • @asgs, I think he means writing same class in two different physical files, which get compiled into one class. – Sanjeevakumar Hiremath Mar 15 '11 at 08:45
  • @Sanjeevakumar: it's difficult to tell if he's asking about the source (`.java`) or the compiled classes (`.class`). – Joachim Sauer Mar 15 '11 at 08:45
  • 1
    @Joachim, I use this feature in C# all the time [`partial`](http://msdn.microsoft.com/en-us/library/wa80x488.aspx) to write UI code. I dont know in Java. I thought that is what is asked here. – Sanjeevakumar Hiremath Mar 15 '11 at 08:47
  • @Sanj: yes, I've read about this. And as far as I know it *only* splits the source (which is not bad, of course). In my opinion if a language needs that, then there's something wrong with the frameworks: why should halve of my file be generated and the other halve be hand-written? I like my separations to be at the class level (for example generate a base class, hand-write the extending class). But that's just my personal opinion. – Joachim Sauer Mar 15 '11 at 08:52
  • Yup, there are tradeoffs and you could still achieve this purity, its just an additional option when you really need it. :) – Sanjeevakumar Hiremath Mar 15 '11 at 08:54
  • 2
    @Joachim: Using inheritance for the separation creates some really ugly problems, in my experience. For example, how should the generated code create a new instance of the same class? It would need to know which user-generated subclass to create an instance of... basically inheritance has all kinds of issues, and using it to solve the problem of mixing manual code and autogenerated code isn't a great idea IMO. – Jon Skeet Mar 15 '11 at 08:58
  • 1
    @Jon: interesting. I never understood the need for this feature (and I'm not at all at home in the .NET world). But your "thumbs up" must mean that it's worth looking into ;-) – Joachim Sauer Mar 15 '11 at 09:01
  • Aspectj can be the anwser to C# partial class feature! Spring Roo is one of the typical development framework using aspectj to divide a class functionatlities into – abgalphabet Feb 13 '13 at 17:06

7 Answers7

43

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    @Basic: I disagree. While partial types are certainly useful in C#, they're far from the most important thing in C# which is missing from Java. If it had been a "huge oversight" in Java you'd have thought the C# designers would have included it in C# 1 - but it was only introduced in C# 2. – Jon Skeet Mar 15 '13 at 06:50
13

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.

  • 2
    Prepostrous! +1 – kyrill Apr 10 '19 at 21:35
  • Very creative suggestion. I do, however had, think that this is too much in regards to the question - I assume the user wanted to know a simple way how to load code in files specifically, as may be known from ruby, python and so forth. – shevy Mar 29 '20 at 00:46
  • 1
    @Graf, but interfaces do not have fields so the methods done this way had to be stateless. Yet the main reason to split class file into multiple files is due to having too many private fields. – Pacerier May 15 '20 at 20:15
  • Good proposal, however static class, methods can not be used. – Gico Apr 25 '22 at 12:32
11

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.

Community
  • 1
  • 1
spa
  • 5,059
  • 1
  • 35
  • 59
5

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.

sleske
  • 81,358
  • 34
  • 189
  • 227
  • I am not sure if that would be cumbersome at all. For example, I use ruby already to compile the different .java files, so to me this would be a trivial thing to do - but I lack knowledge about which commands would be required to do so right now. – shevy Mar 29 '20 at 00:47
3

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

biziclop
  • 48,926
  • 12
  • 77
  • 104
2

No, in Java this can not be done.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
0

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

mckulpa
  • 374
  • 4
  • 9
  • 7
    @byyyk: Size isn't the only reason to want to split the source up. If you've got some generated code and some "manual" code, it's useful to be able to separate the two but still build a single class. – Jon Skeet Mar 15 '11 at 08:47
  • @Jon there are probably better ways to deal with that use-case. – Stephen C Mar 15 '11 at 08:53
  • 2
    @Stephen C: Well I've found partial classes have worked *really* well in C# for generated code. For example, it allows Protocol Buffer serialization classes to add conversions for other types, etc. It works well for GUI designers too, in my experience. – Jon Skeet Mar 15 '11 at 08:55
  • @Jon Skeet They do sound really useful, but also dangerous in the wrong hands. – biziclop Mar 15 '11 at 22:59
  • 1
    @biziclop: It certainly *could* be abused, but I've seen less abuse of this feature than some others. – Jon Skeet Mar 16 '11 at 06:33
  • @Jon Skeet I didn't mean it in a negative way, I believe potential for abuse is also a rough indicator of how powerful a feature is. – biziclop Mar 16 '11 at 10:29
  • @biziclop example of abused? I think it is useful as it can partial static member. So I can write c# with a lot of function. – bronze man Jun 28 '19 at 11:33
  • I agree with Jon Skeet. There are numerous useful use cases for it. I believe the proper answer would have been that this is not easily doable in java, rather than speculate about use cases. For example, in ruby I totally split code into separate .rb files, simply because it is much easier for me to handle this (e. g. I often have a method such as run() or reset(), so I like to put them into separate files if the class becomes too big; and having only tiny classes is not always possible.) – shevy Mar 29 '20 at 00:49