Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

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.

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.

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() {
        this(System.currentTimeMillis());
    }

    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 {

    @Test
    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 {

    @Test
    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.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments