Possible to Use Two Java Classes with Same Name and Same Package

I have two classes with same name and same package structure in different jars. Is it possbile to choose a class from jar specifically?

My question is, Is it possible to create object based on constructor.

I take you to be asking whether Java will choose from which jar to load the class based on which version of the class has a constructor matching the given signature. No, it will not. Java will look for the designated class in the directories and jars in the classpath, in order, and will use the first one it finds.

It is conceivable that you could write a custom ClassLoader that would assist you in choosing which version of the class to load based on the signatures of available constructors, but such capabilities would not trigger automatically, as you seem to want. Java loads the class before it considers which constructors are available in it. You would need to load the class manually, and probably to instantiate it reflectively. And your problems wouldn't end there. All of this is almost certainly much more trouble than it's worth.

How about writing a subclass instead of a different version of the same class?

Java - Two classes, same name, same package but in different folders

No, this is not possible. Java will not "look to find the method", it will resolve a single .class file by fully-qualified name, look for the method there, and throw an exception if it's not there. You need to find a different way to split your code into multiple classes.

Importing 2 different classes with same name and packages from 2 different modules

First this sounds like a design flaw to me cause having the same package names sounds that you haven't separated on the package level which you already did on the maven module level so not representing that same segregation level on the package level as well. This will usually happen if you have different groups of classes...apart from that If you have the exact same package and class name that is simply not possible cause a classname must be unique within a package which is violated here. The result will be having two different JAR's on classpath which offer the same class. The class which is really used depends on which JAR is first on the classpath which is by coincidence so not predictable.

OSGi will not help here cause you have the same problem here cause you would like to access the same class from different bundles which is not possible to solve. Furthermore in JDK 9 this will not work either cause those classes have to be made public (same as in OSGi) which will fail as well.

Same class name in different packages

Yes, you can have two classes with the same name in multiple packages. However, you can't import both classes in the same file using two import statements. You'll have to fully qualify one of the class names if you really need to reference both of them.


Example: Suppose you have

pkg1/SomeClass.java

package pkg1;
public class SomeClass {
}

pkg2/SomeClass.java

package pkg2;
public class SomeClass {
}

and Main.java

import pkg1.SomeClass;   // This will...
import pkg2.SomeClass; // ...fail

public class Main {
public static void main(String args[]) {
new SomeClass();
}
}

If you try to compile, you'll get:

$ javac Main.java
Main.java:2: pkg1.SomeClass is already defined in a single-type import
import pkg2.SomeClass;
^
1 error

This however does compile:

import pkg1.SomeClass;

public class Main {

public static void main(String args[]) {
new SomeClass();
new pkg2.SomeClass(); // <-- not imported.
}
}

JAVA: Conflicts with Same Package and class names present under different Source folders

The identity of a Java class is its "absolute" name, as in x.y.z.SomeClass. When you have two classes that have absolute identical package and class names, then "java" isn't able to distinguish them.

In other words: this is bad practice. You really want to avoid such situations. The solution is not to work around "my tool is complaining" but organize your source code in ways that are more in line with "standard java conventions".

In other words: the best thing to do would be to either rename some of the packages, or make sure to have unique class names.

how to import java classes with the same names and same namespace (package names) into one maven projec

This cannot be done. Java cannot distinguish two classes with the same qualified name.

You need to rename one of the packages. If at least one of the projects abc and def is yours, this can be easily done. If not, You probably need something like the Maven shade plugin.

2 Classes with the same name, serving different purposes

I'm not entire sure what the question here is. Do you want to know if it's possible to give 2 classes the same name, or just whether it's a bad idea?

A convention that is often used for classes that are meant to model database entities, is to postfix the classname with Entity. So you could name the first class Call and the second CallEntity. This removes some ambiguity about the classes purposes. Most professional developers will also immediately make the assumption that the Entity class is supposed to represent something that is persisted.

However if you really insist on giving both classes the same name. That's perfectly possible, if you put them in separate packages. The package you put them in can also provide more clarity about the intent of the class. The first could be domain.model.Call, while the second could be domain.entity.Call

Hope this is somewhat helpful :)

Import classes with the same name

Importing two classes with the same name and then trying to use them won't work, because the compiler can ofcourse not know which one of the two classes you mean. You'll have to use the fully-qualified name of at least one of the classes instead of importing it.

For example:

package whatever;

import predictor.version1.Predictor;

public class Result {
// Uses the Predictor from package predictor.version1 which was imported
public void prediction(Predictor p) {
// ...
}

// Use fully-qualified class name to indicate that Predictor
// from package predictor.version2 should be used
public void prediction(predictor.version2.Predictor p) {
// ...
}
}


Related Topics



Leave a reply



Submit