Renato Dinhani
Renato Dinhani

Reputation: 36686

In Java, it's possible have two or more files for the same class?

I want to know if is possible have two or more files for the same class (not different classes). I want this for better organization of the code.

I think that the answer is "No!", but maybe, someone knows an obscure way to do this.

The idea is having a file that is a configuration GUI called AnalysisConfiguration and another class that does a bridge between the GUI and Apache Commons Configuration(setting from the GUI or loading the configurations to the GUI).

I think the two can be the same class, but I think the code comes dirty mixing the business configuration methods with the "GUI-To-File" methods. I can easily call the second class something like AnalysisConfigurationFileBridge, but the curiosity tell me to search if is possible having the two as same class.

Upvotes: 2

Views: 322

Answers (6)

Peter Lawrey
Peter Lawrey

Reputation: 533510

What you can do is have several abstract classes as parents. You can have

abstract class A {
    // some methods and fields.
}

abstract class B extends A {
    // some more methods and fields.
}

class C extends B {
    // some more methods and fields.
}

If having loads of classes seems crazy, it may just be crazy. ;) You should use as many or as little classes as you think makes the most sense.

Upvotes: 0

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

Java does not have ability to code partial classes. Some other languages, for example C#, do. Furthermore, mixins are not supported in Java, so together certain coding patterns become cumbersome. You may find that one part extending the other is sufficient. Otherwise, you can look at various options including byte-code manipulation, meta programming or simply use another JVM language as needed.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691725

The answer is indeed no. If the code of a class is so long that it would need two files to be better organized, it's probably the sign that it should be refactored and split into several classes.

Upvotes: 3

digitaljoel
digitaljoel

Reputation: 26574

You can use AspectJ's inter-type declarations to put some implementation in one aspect j file and the rest in your java file and have them magically combined at compile time.

I know Spring Roo uses this for getters and setters, the idea being that having that boilerplate code out of the .java file makes it cleaner.

See http://www.eclipse.org/aspectj/doc/released/progguide/language-interType.html

Upvotes: 1

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

No, but it is possible to have more than one class in the same source file. You can also use some preprocessor, which would join distinct several source files into one before handling it to javac, but this would add extra build step.

Decent IDE like IntelliJ IDEA would help you more

Upvotes: 0

home
home

Reputation: 12538

No, not in the same package. The name of a .java file must always match the name of it's top level (public) class.

Upvotes: 1

Related Questions