Yes, you can have two classes with the same name in multiple packages. However, you cannot import both classes into the same file using two import statements. You will need 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 will get:
$ javac Main.java Main.java:2: pkg1.SomeClass is already defined in a single-type import import pkg2.SomeClass; ^ 1 error
This, however, compiles:
import pkg1.SomeClass; public class Main { public static void main(String args[]) { new SomeClass(); new pkg2.SomeClass();
source share