Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing two classes with same name. How to handle?

Tags:

java

People also ask

Can two classes have same name?

Error. In fact, you can't create two public classes in a single file, Only one class should be public and it should be the name of the class. If you try to create two public classes in same file the compiler generates a compile time error.

Can we have two classes with same name under the same package?

Yes there is. You would need to implement your own Classloader and play some games to be able to access both during runtime.

Can we have two classes declared with same name in Java?

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public.

Can we give fully qualified class name instead of importing that class if yes how do you do that?

If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface. It is generally used when two packages have same class name e.g. java.


You can omit the import statements and refer to them using the entire path. Eg:

java.util.Date javaDate = new java.util.Date()
my.own.Date myDate = new my.own.Date();

But I would say that using two classes with the same name and a similiar function is usually not the best idea unless you can make it really clear which is which.


use the fully qualified name instead of importing the class.

e.g.

//import java.util.Date; //delete this
//import my.own.Date;

class Test{

   public static void main(String [] args){

      // I want to choose my.own.Date here. How?
      my.own.Date myDate = new my.own.Date();

      // I want to choose util.Date here. How ?
      java.util.Date javaDate = new java.util.Date();
   }
}

Yes, when you import classes with the same simple names, you must refer to them by their fully qualified class names. I would leave the import statements in, as it gives other developers a sense of what is in the file when they are working with it.

java.util.Data date1 = new java.util.Date();
my.own.Date date2 = new my.own.Date();

Another way to do it is subclass it:

package my.own;

public class FQNDate extends Date {

}

And then import my.own.FQNDate in packages that have java.util.Date.


If you have your own date class you should distinguish it form the built in Date class. i.e. why did you create your own. Something like ImmutableDate or BetterDate or NanoDate, even MyDate would indicate why you have your own date class. In this case, they will have a unique name.


You can import one of them using import. For all other similar class , you need to specify Fully qualified class names. Otherwise you will get compilation error.

Eg:

import java.util.Date;

class Test{

  public static void main(String [] args){

    // your own date
    my.own.Date myOwndate ;

    // util.Date
    Date utilDate;
  }
}

This scenario is not so common in real-world programming, but not so strange too. It happens sometimes that two classes in different packages have same name and we need both of them.

It is not mandatory that if two classes have same name, then both will contain same functionalities and we should pick only one of them.

If we need both, then there is no harm in using that. And it's not a bad programming idea too.

But we should use fully qualified names of the classes (that have same name) in order to make it clear which class we are referring too.

:)