18

C# has this great concept where a class can be spread across multiple .cs files. This works great where you want a single object (member variables that all the code needs) but there's a ton of code. You can then spread this code out by functionality across source files.

Is there a way to do this in Java?

Update: Ok at first I told myself that this must be a single large class (it performs layout of the content of a DOCX file). But then after posting this I thought about it more and it really bothered me that it is this one large (5,000+ lines at present) class.

So I thought through some alternatives and came up with a good way to break it out into one main class and about 20 helper classes. It works very well this way really separating out the functionality into each part.

So... while I think partial classes is a useful construct at times, in this case the lack of partial classes caused me to come up with a better design. (And this has nothing to do with the initial question, but I thought it was worth sharing.)

David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • 1
    It looks like [no](http://stackoverflow.com/questions/9876339/a-way-to-implement-partial-classes-in-java?rq=1) – Chris Forrence Dec 13 '13 at 22:53
  • 8
    If your class is large enough to require multiple files you probably need to refactor it so it doesn't. If I had to maintain it that's the first thing I would do. – Jim Garrison Dec 13 '13 at 22:53
  • 1
    @JimGarrison Often true, but I personally like the feature for (a) the designer and (b) to have a class for static methods and still being able to organize them neatly. It's kind of a feature that you should never ever overuse, but comes in handy from time to time – Janis F Dec 13 '13 at 22:56
  • possible duplicate of [partial classes/partial class file](http://stackoverflow.com/questions/2764234/partial-classes-partial-class-file) – LAFK 4Monica_banAI_modStrike Dec 13 '13 at 23:03
  • Wow +5K lines. well to organise your code i hope you know //region ... //endregion ... (Java / gradle files) OR for android studio XML files (manifests, layouts...) OR #region #endregion for Progard files? Example ... – TwiXter Mar 24 '23 at 09:18

5 Answers5

16

No. Java does not support partial classes. You'll find more in depth discussion in this question and this question.

Community
  • 1
  • 1
Asaph
  • 159,146
  • 25
  • 197
  • 199
7

No, Java doesn't support partial classes.

If this is just idle curiosity, check out Scala. It compiles to .class files just like Java, and has full interop. It supports a rough equivalent of partial classes as "traits".

An example of trait usage:

trait FunctionalityA {
  // some stuff implemented here
}

trait FunctionalityB {
  // more stuff implemented...
}

// etc...

class MyBigClass extends FunctionalityA with FunctionalityB with FunctionalityC
Dylan
  • 13,645
  • 3
  • 40
  • 67
  • Scala also supports implicit classes to add methods to existing classes, but this really works by wrapping existing objects, so it has a cost in time and memory at runtime. – Robin Green Dec 14 '13 at 00:02
1

I find a way to emulate C# partial class depend on @Delegate in lombok

@Getter 
@Setter
public class User {
    String name;
    @Delegate
    private UserDelegate delegate= new UserDelegate(this);
}
public class UserDelegate {
  private User expandOwner;

  public UserDelegate(User expandOwner) {
    this.expandOwner = expandOwner;
  }

  public void doSomethinga() {
    System.out.println(expandOwner.getName() + "did something!");
  }
}
// how to use:
User user= new User();
user.setName("Chrisme");
user.doSomethinga();

you can access original class's public method via expandOwner in delegate class.

pradosh nair
  • 913
  • 1
  • 16
  • 28
chrvip
  • 11
  • 1
0

There is no way to have partial class definitions, spread across files. Every class must be defined in its own namesake file.

On the contrary, you can define additional classes within that file and within the (top level) class definition.

PNS
  • 19,295
  • 32
  • 96
  • 143
-4

No, it is one of the wonderful features in Java. A Class must be identical with her filename. :-)