简体   繁体   中英

Same class name in different packages

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 import statements. 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

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'll get:

$ javac Main.java
Main.java:2: pkg1.SomeClass is already defined in a single-type import
import pkg2.SomeClass;
^
1 error

This however does compile:

import pkg1.SomeClass;

public class Main {

    public static void main(String args[]) {
        new SomeClass();
        new pkg2.SomeClass();   // <-- not imported.
    }
}

Sure can but you'll need to distinguish which one you want when calling them in other packages if both are included within a source file.

Response to Comment:

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

i was taken to this page by google when i had the 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) by realising the line import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; had snuck into the very top of my imports whilst i had the line import org.apache.commons.codec.binary.Base64; at the bottom of my imports.

So I was looking for a smarter solution than just using fully qualified names on one or both of the implemented classes.

If you create a private class, and extend your class, you are free to use the 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 { }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM