Java different package name the same class name conflict can only rename the class name?

< H2 > scenario 1 < / H2 >

suppose:

one
    Test.java
two
    Test.java
App.java

such a directory structure. App.java is the entry, and the content is as follows:

//  one.Test  two.Test 
import one.Test;
import two.Test;

public class App {
    public static void main(String[] args){
        //  one.Test 
        //  two.Test 
    }
}

in the case of the above, how to solve the problem of different package names with the same class name? Can we only rename the conflict class? Or do you quote it in a full-path way?

Jul.29,2021

you have said a good solution, and another thing that is not very good is that custom classloader


custom classloader can be solved.


because you have two identical object names, both Test, and import, when the java compiler does not know which one you are calling. So if two classes have the same name, one uses the import, and the other uses the full name. There is no problem with import for different class names.


Java does not have renaming such as using , typedef , so you can only write the package name in its entirety.

Menu