Skip to content
Advertisement

Importing two classes with same name. How to handle?

Say I’ve a code like:

import java.util.Date;
import my.own.Date;

class Test{

  public static void main(String [] args){

    // I want to choose my.own.Date here. How?
    ..
    // I want to choose util.Date here. How ?

  }
}

Should I be full qualified class names? Can I get rid of the import statements? Is such a scenario common in real world programming?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement