DEV Community

Mercy
Mercy

Posted on

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.

Top comments (1)

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise

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