2

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.

4
  • What would you hope to achieve by doing this? It honestly sounds like you should look at refactoring your class and improving your design. Commented Sep 6, 2011 at 19:39
  • Even if a way to do this exists, it's probably obscure precisely because it's not better organization. Commented Sep 6, 2011 at 19:39
  • Possible duplicate stackoverflow.com/questions/2764234/… ? Commented Sep 6, 2011 at 19:41
  • @Thomas Look at my edited question, I put my situation and what I want to do. Commented Sep 6, 2011 at 19:54

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Long classes don't necessarily mean refactoring. Take a look at Java's String class. It's a pretty lengthy class. Instead, you should look at measures and metrics such as coupling, cohesion, and fan-in/fan-out when looking to refactor.
@Thomas: agreed. In French, "certainement" (the literal translation of certainly) often actually means "probably". I think it's not the case in English, but my brain thinks in French. I edited my answer.
1

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

Comments

1

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

Comments

0

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.

Comments

0

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

1 Comment

Having more than one class in the same source file has got nothing to do with the OP's question.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.