Can same class exist in multiple packages?
In other words, can I have Foo.java class in com.test.package1 and com.test.package2?
Update
Now I copied class from package 1 and placed in to package 2 and now I am creating an instance of that class, I want this instance to point to class present in package 1 but currently it points to package1 path, how can i modify it?
Oh so I cannot do something like:
Foo = new Foo() // pointing to Foo class in package 1
Foo = new Foo() // pointing to Foo class in package 2
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
importstatements. 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
pkg2/SomeClass.java
and Main.java
If you try to compile, you’ll get:
This however does compile: