DEV Community

Mercy
Mercy

Posted on

3 1 1 1 1

Redundant imports and Naming conflicts in Java

👋Hello friends and family, how are you all doing? In today's article, we are gonna be talking about redundant imports and naming conflicts in Java.

What are redundant imports
It refers to the duplication of another import. This is when a class is imported more than once. There’s one special package in the Java world called java.lang. This package is special in that it is automatically imported. You can still type this package in an import
statement, but you don’t have to

Below is an example of redundant imports

import java.lang.System; //Redundant
import java.lang.*; //Redundant
import java.util.Random;
import java.util.*; //Redundant
public class ImportExample {
   public static void main(String[] args) {
      Random r = new Random();
      System.out.println(r.nextInt(10));
   }
}
Enter fullscreen mode Exit fullscreen mode

In the example three of the imports are redundant. Line 4 is also
redundant in this example because Random is already imported from java.util.Random.

Another case of redundancy involves importing a class that is in the same package as the
class importing it. Java automatically looks in the current package for other classes.

Naming Conflicts
One of the reasons for using packages is so that class names don’t have to be unique across
all of Java. This means you’ll sometimes want to import a class that can be found in multiple places. A common example of this is the Date class. Java provides implementations of java.util.Dateand java.sql.Date.

Read more about redundant imports and naming conflicts here
Make sure to continue reading and practicing coding, as practice leads to progress.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (2)

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise •

The simplest solution is to use full qualified imports (every IDE supports that out-of-the-box)...

Collapse
 
devmercy profile image
Mercy •

Sure, that was Java 8 before all these advanced versions

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay