The same class name in different packages

Can the same class exist in multiple packages? In other words, can I have the dummy.java class in com.test.package1 and com.test.package2 ?

Update

Now I have copied the class from package 1 and placed it in package 2, and now I am creating an instance of this class, I want this instance to point to the class present in package 1, but it currently indicates the path of package1, how can I change this is?

So I can not do something like:

 Foo = new Foo() //pointing to 1 package Foo class Foo = new Foo() // pointing to 2 package Foo class 
+17
source share
4 answers

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(); // <-- not imported. } } 
+35
source

Sure, but you will need to distinguish the one you want when you call them in other packages, if both are included in the source file.

Reply to comment:

 com.test.package1.Foo myFoo = new com.test.package1.Foo(); com.test.package2.Foo myOtherFoo = new com.test.package2.Foo(); 
+4
source

i was sent to this google page when I had an error a type with the same simple name is already defined by the single-type-import . I fixed this error (AFTER A VERY LONG TIME), realizing that the line import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; penetrated the very top of my import, while I had the line import org.apache.commons.codec.binary.Base64; at the bottom of my import.

+2
source

Therefore, I was looking for a more reasonable solution than just using fully qualified names in one or both of the implemented classes.

If you create a private class and extend your class, you can use this class without writing the full package name each time.

Package 1

  namespace namespace1.Logger { public class Log { public void Foo1(){} } } 

Package 2

  namespace namespace2.Logger { public class Log { public void Foo2(){} } } 

My class implementation

  //using namespace1.Logger; //using namespace2.Logger; namespace MyProject { public class MyClass { public MyClass() { LoggerA a = new LoggerA(); LoggerB b = new LoggerB(); a.Foo1(); b.Foo2(); } private class LoggerA : namespace1.Logger.Log { } private class LoggerB : namespace2.Logger.Log { } } } 
0
source

All Articles