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.)
A partial class, or partial type, is a class that can be split into two or more source code files and/or two or more locations within the same source file. Each partial class is known as a class part or just a part. Logically, partial classes do not make any difference to the compiler.
There can be zero or more partial class definitions for every full definition of a class. Every partial class definition of a class must lexically precede the one full definition of that class, but doesn't have to precede forward declarations of the class.
A partial class is created by using a partial keyword. This keyword is also useful to split the functionality of methods, interfaces, or structure into multiple files.
No, you cannot have two partial classes referring to the same class in two different assemblies (projects). Once the assembly is compiled, the meta-data is baked in, and your classes are no longer partial. Partial classes allows you to split the definition of the same class into two files.
All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project.
Partial Class is a unique feature of C#. It can break the functionality of a single class into many files. When the application is compiled, these files are then reassembled into a single class file. The partial keyword is used to build a partial class.
No. Java does not support partial classes. You'll find more in depth discussion in this question and this question.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With