138

Say I've 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 using full qualified class names? Can I get rid of the import statements? Is such a scenario common in real world programming?

Pang
  • 9,564
  • 146
  • 81
  • 122
gameover
  • 11,813
  • 16
  • 59
  • 70
  • 1
    Not really an answer to your question but in C# you may use an alias for any namespace. May be it is just syntactic sugar but it is really helpfull: http://msdn.microsoft.com/en-us/library/7f38zh8x.aspx – borjab Apr 11 '14 at 09:38

12 Answers12

191

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.

Ellie P.
  • 4,133
  • 3
  • 30
  • 30
  • 2
    If you're using Eclipse, you can change the name of `your.own.Date` using ctrl+shift+R. This will automatically change it everywhere you refer to it in your code, as well as in the file (and filename) your/own/Date.java. Any other IDE probably has a similar feature. – Tyler Jan 17 '10 at 04:14
  • 25
    I don't agree with last statement. If you want to design your own Date class, the `Date` is the perfect name. You will use it in most of your code. However, sometimes you will need to call the `java.util.Date` notably to make conversions between both. – paradigmatic Jan 17 '10 at 04:37
  • 2
    @MatrixFrog The feature in Eclipse that you have specified is also provided by Netbeans IDE. This feature is known as "Refactor". Your info wasn't wrong but it is not the answer of the question asked. If he (Roger) is developing that code, then definately he knows that he can change or refactor the name of his Class. What he is asking is different from the answer you gave. – Amit Jan 17 '10 at 07:58
  • 11
    @Yatendra That's why I added it as a comment rather than an answer. I was expanding on the point Ellie P. made at the end of her answer. Roger probably knows that, but the point of SO is to help other developers, not just the person who asked the question. If people don't know about the IDE feature, they might think that it's unfeasible to switch names by hand, so I thought it would be useful to throw in that info. – Tyler Jan 17 '10 at 08:19
  • 6
    My most frequent name clash occurs with `org.apache.log4j.Logger` and `java.util.logging.Logger`. Usually, I have no control over one side or the other; I'm doing legacy code integration. – kevinarpe Jul 01 '15 at 11:07
  • 6
    a new question is why java don't have an "import as" – John Woo Aug 28 '16 at 02:25
  • 1
    This works well when the path is short. In my cases I've long package names and typing the full qualified name exceeds the column limit – Christian Vincenzo Traina Apr 28 '22 at 16:57
26

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();
   }
}
Chii
  • 14,540
  • 3
  • 37
  • 44
10

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();
D.C.
  • 15,340
  • 19
  • 71
  • 102
7

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.

Cheruvim
  • 127
  • 1
  • 5
  • I do like this except (it is simple) however it doesn't address the issue for say accessing static methods. – Justin Ohms Oct 14 '13 at 22:57
  • I do this all the time when I want to use Hamcrest `Matchers` and Mockito `Matchers` in the same class. It seems to work with static methods. – Adam Burley Feb 03 '16 at 21:39
  • @Kidburla you can also use static imports so long as you don't care which matcher comes from where. I often do this in unit tests for matchers and `.when`s, `.thenReturn`s etc. - removes the `Mockito.` bloat. – CptBartender Apr 01 '16 at 10:02
  • 2
    This is a bad practice. Classes shouldn't be extended unless some functionality is being extended from the original class. – Partha Feb 11 '20 at 06:12
6

If you have your own date class you should distinguish it from 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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
5

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

Example:

import java.util.Date;

class Test{

  public static void main(String [] args){

    // your own date
    my.own.Date myOwndate ;
    
    // util.Date
    Date utilDate;
  }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Anuraj
  • 2,551
  • 21
  • 26
5

If you really want or need to use the same class name from two different packages, you have two options:

1-pick one to use in the import and use the other's fully qualified class name:

import my.own.Date;

class Test{

     public static void main(String[] args){

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

        // I want to choose util.Date here. How ?
        //Answer:
        java.util.Date utilDate = new java.util.Date();

     }
}


2-always use the fully qualified class name:

//no Date import
class Test{

  public static void main(String[] args){

    // I want to choose my.own.Date here. How?
    //Answer:
     my.own.Date ownDate = new my.own.Date();
    // I want to choose util.Date here. How ?
    //Answer:
     java.util.Date utilDate = new java.util.Date();

  }
}
manfall19
  • 428
  • 6
  • 5
3

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.

:)

Amit
  • 33,847
  • 91
  • 226
  • 299
2

I hit this issue when, for example, mapping one class to another (such as when switching to a new set of classes to represent person data). At that point, you need both classes because that is the whole point of the code--to map one to the other. And you can't rename the classes in either place (again, the job is to map, not to go change what someone else did).

Fully qualified is one way. It appears you can't actually include both import statements, because Java gets worried about which "Person" is meant, for example.

1

When you call classes with the same names, you must explicitly specify the package from which the class is called.

You can to do like this:

import first.Foo;

public class Main {
    public static void main(String[] args) {
        System.out.println(new Foo());
        System.out.println(new second.Foo());
    }
}



package first;

public class Foo {
    public Foo() {
    }

    @Override
    public String toString() {
        return "Foo{first class}";
    }
}



package second;

public class Foo {
    public Foo() {
    }

    @Override
    public String toString() {
        return "Foo{second class}";
    }
}

Output:

Foo{first class}
Foo{second class}
Kirilo Lozitsky
  • 153
  • 2
  • 7
1

If you have to use two classes with same name but different packages in the same file, there is no other option than using fully qualified name for at least one of the two classes. I'm more and more wondering if we shouldn't, in such cases, use fully qualified names all the times, so it would be clearer which class is which, but that makes the code harder to parse because of increased verbosity. Unfortunately, more and more programmers make this common by duplicating all data objects: one copy generated by Protoc in case of GRPC, one copy for JSON serialization, one copy for internal logic, one copy for Hibernate, etc., without any automated way of mapping one to another, plain old code duplication all over the place. IDEs are not well-designed for that, showing just com.abc.myproject.... instead of the fully qualified names when trying to use code completion. Unfortunately, Java offers no good solution by itself, IDEs have to jump in with auto-complete or auto-import. Here we would need aliases as offered by C#, Python and Javascript.

Eric Buist
  • 73
  • 5
0

I just had the same problem, what I did, I arranged the library order in sequence, for example there were java.lang.NullPointerException and javacard.lang.NullPointerException. I made the first one as default library and if you needed to use the other you can explicitly specify the full qualified class name.

Bondhan Novandy
  • 362
  • 1
  • 4
  • 16