Handle Classes With the Same Name in Java – 在Java中处理同名的类

最后修改: 2022年 9月 5日

1. Introduction


Class naming in Java follows an international convention called Upper Camel Case syntax, like the major programming languages. However, when it comes to handling classes with the same name, there’s a challenge.

在Java中,类的命名遵循一种叫做Upper Camel Case语法的国际惯例,和主要的编程语言一样。然而,在处理同名的类时,有一个挑战。

Since the early release of JDK in 1998, there’s been a debate about how to solve this unusual situation. Here’s JDK-4194542, the first opened bug on this topic, and since then, the JDK development team’s recommendation has been to use the fully-qualified class names. Nevertheless, there are no plans for the JDK to have any shortly a feature that allows this kind of usage.

自 1998 年发布 JDK 的早期版本以来,人们一直在讨论如何解决这种不寻常的情况。这里有JDK-4194542,这是关于这个主题的第一个公开的错误,从那时起,JDK 开发团队的建议是使用完全限定的类名。然而,目前还没有计划让 JDK 在短期内拥有允许这种用法的功能。

Lately, in August 2019, the Java developers community raised a new proposal (JEP) on how to solve this situation, and it’s gaining more support from Java developers worldwide.


In this tutorial, we’ll discuss strategies and recommendations for dealing with classes with the same name.


2. Defining the Class


First, let’s create a class called Date inside a custom package com.baeldung.date.


package com.baeldung.date;

public class Date {

    private long currentTimeMillis;

    public Date() {

    public Date(long currentTimeMillis) {
        this.currentTimeMillis = currentTimeMillis;

    public long getTime() {
        return currentTimeMillis;

3. Fully Qualified Class Names


We’ll use this approach to avoid collisions when this type of usage is isolated and not frequently repeated. Nevertheless, using fully qualified names is usually considered a poor style.


Let’s look at how to use it, especially if the package name is short and descriptive could make the code more expressive, therefore reducing confusion and increasing readability.


On the other hand, it helps to debug when the objects inside were used are too large classes or methods:


public class DateUnitTest {

    public void whenUsingFullyQualifiedClassNames() {

        java.util.Date javaDate = new java.util.Date();
        com.baeldung.date.Date baeldungDate = new com.baeldung.date.Date(javaDate.getTime());

        Assert.assertEquals(javaDate.getTime(), baeldungDate.getTime());

4. Import the Most Used One


We import the one we most use and utilize the least used one with a full classpath, as this is the common technique and a best practice among Java developers:


import java.util.Date;

public class DateUnitTest {

    public void whenImportTheMostUsedOne() {

        Date javaDate = new Date();
        com.baeldung.date.Date baeldungDate = new com.baeldung.date.Date(javaDate.getTime());

        Assert.assertEquals(javaDate.getTime(), baeldungDate.getTime());

5. Conclusion


In this article, we illustrated the two possible approaches regarding the use of classes having the same name depending on particular situations and observed the main difference between them.


As always, the complete code samples for this article can be found over on GitHub.