Importing two classes with same name. How to handle?

java two classes with same name and package
package clashes with class of same name
java same package name in different jars
java import class in same folder
java import multiple classes from same package
java import class from another package
java import class with another name
a type with the same simple name is already defined by the single type import of document

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?

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.

Change Name of Import in Java, or import two classes with the same , There is no import aliasing mechanism in Java. You cannot import two classes with the same name and use both of them unqualified. Import  call the full class name, i.e. package.class. for instance, if you would create some class named File, you could still use without importing it. imports only lets you use the shorter name. a class full name is prepended by it's package name . in your case, you have: some.user.package.Logger, so you could use it exactly like that.

use the fully qualified name instead of importing the class.


//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();

Same class name in different packages, 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  I want to import these two classes, both named Query - one a JDO class, the other a JPA class, to use in different methods in the same class. import javax.jdo.Query; import javax.persistence.Query; Is there a way to globally import both of them at th. Two classes with the same name in classpath.

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();

How to deal with Classes having the same name (different packages , Use the package name. This type of problem is precisely why Java uses the package naming convention that it does. It prevents these sorts of problems,  Therefore, if you have two classes with the same name then you can import only one and you will have to use the fully qualified name for the other. Importing an entire package If you have multiple classes from the same package that you are planning to use in your code then it would make more sense to import the complete folder instead of all classes individually.

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.

Using Imports in Java and importing packages, How do you import a package ? can you import same name classes? what is a fully qualified name in java? Importing two classes in different packages but with the same name. Let's say you How would you handle this? If you have source files that deal with both worlds, they might be bridging classes with the resonsibility to translate between two different views of the world, and here I'd prefer to find package-prefixed class names.

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.

Import duplicate class name .. different package (Beginning Java , If you want to import two CLASSES with the same name instead you will have to use the full qualified class name for one of them in your code. so refer to them with their full names. Another option is to rename the imported name with as: from ShutterDeck.Controller import User as ControllerUser from ShutterDeck.Helper import User as HelperUser u1 = ControllerUser.User() u2 = HelperUser.User() share. Share a link to this answer.

Packages, The members of a package are class and interface types (§7.6), which are A package may not contain two members of the same name, or a compile-time error results. If two single-type-import declarations in the same compilation unit attempt to Here are some suggested conventions for dealing with these situations:. Conversely, if your classes with the same name all have the same or very similar APIs, then that's a good example of when you should use the same name with different namespaces. The Scala example is precisely this, where all the Map classes implement the same interface or are subclasses of each other.

Which classes in Java have the same name but in different , In other words, import explicitly every class you use, don't import with wildcards. How can I handle the confusion caused by using two different programming  Let's say we have two different classes with the same name, in two different Jars: The first one is located in Lib1.jar, the second one is located in Lib2.jar. Both are in the classpath.

Packages, Classes in different packages can have the same name. For example, you have the two classes java.util.Date and java.sql.Date which are not the same. Name Conflicts: We can define two classes with the same name in different packages so to avoid name collision, we can use packages Types of packages in Java As mentioned in the beginning of this guide that we have two types of packages in java.

  • 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:
  • 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/ Any other IDE probably has a similar feature.
  • 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.
  • @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.
  • @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.
  • 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.
  • Best practice is to import the most used one, using the least used one with full the classpath
  • I do like this except (it is simple) however it doesn't address the issue for say accessing static methods.
  • 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.
  • @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 .whens, .thenReturns etc. - removes the Mockito. bloat.
  • This is a bad practice. Classes shouldn't be extended unless some functionality is being extended from the original class.
  • Please include the code from the screenshot in your answer.