How can I access two classes with the same name in different packages?
foo.bar.myClass.class
and
foo.myClass.class
All of this in the same class
@TestRunner(Suite.class)
@SuiteTest({bar.myClass.class, myClass.class})
Thank you.
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.
If you create another class with same name it will be a duplicate class. Still if you try to create such class then the compiler will generate a compile time error.
One would have the same problem importing those classes in Java code. It's just not possible to import two classes with the same name, because there is no way to tell which one you are referring to when you use the class' name only (e.g. (Application.)). (Application.) (javafx.
Technically you can have a same class with same content in two different packages but then when you use these 2 classes in another Java class then you would have to be very specific (absolute package name) when using either of the class.
You need to use the fully qualified names of the classes.
foo.bar.myClass myvar;
foo.myClass anothervar;
you will have to import one and other you will be writting fully qualified path
for example in your code:
import foo.bar.myClass;
.
.
.
myClass ob; // this will refer to foo.bar.myClass
foo.myClass ob1 ;//this will refer to foo.myClass
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