sábado, 18 de julio de 2020

Naming conflicts in Java


@jwimmerli

















In Java there are situations that you have to use a class that have the same name but resides in different packages, for example the Date class, you can find a implementation of the Date class in the package java.util  and in the package java.sql , both classes have similarities but work for different purposes.

What if you have to use both classes ( java.util.Date and  java.sql.Date  ) in a class of your program, how you can deal with the naming collision  that you have if you try to import both classes.

Let's see the following example to have a clear idea of the problem that we are trying to solve:
in the following snippet of code we have a class named Comment that is declaring two variables of type Date:
package mysite;

import java.util.Date;
import java.sql.Date; // this import will throw an error: The import java.sql.Date collides                         // with another import statementJava(268435842)
public class Comment {
private Date date;
private Date dateSql;

public Comment(Date date, Date dateSql){
this.date = date;
        this.dateSql = dateSql;
}

public Date getDate(){
return this.date;
}
public Date getDateSql(){
return this.dateSql;
}
}

As  you can see,  if you try to import a class with the same name, you will have a compiler time error indicating that the second class that your are importing collides with the first class,  in this case java.sql.Date  collides with the  java.util.Date

You can solve this naming conflict in several ways, but I recommend you, if you are having this type of naming collisions, always use the full qualified name declaration to remove ambiguity and make your code easier to read.
In this case, I remove all the explicit imports and declare the variables using FQN, as you can see in the following code snippet:

package mysite;


public class Comment {

private java.util.Date date;
private java.sql.Date dateSql;

public Comment(java.util.Date date, java.sql.Date dateSql){
this.date = date;
this.dateSql = dateSql;
}


public java.util.Date getDate(){
return this.date;
}
public java.sql.Date getDateSql(){
return this.dateSql;
}
}

This is more long typing coding, but this assure that every person that read your code will understand from which package your are using a specific class.


Recommended readings:





viernes, 17 de julio de 2020

Redundant imports in Java


@builtbymath 

















When you are creating a class in Java you can import packages with the "import" clause, for example:

import java.util.Random;

public class Blogger {
public static void main(String[] args) {
Random random = new Random();
System.out.println(random.nextInt(9));
}
}

In this case we are importing the class "Random.java" from the package util.
But java allows us to import multiple packages, even we can have multiple redundant import declarations in a class, as follows:

import java.util.Random;
import java.util.Random; //Redundant import clause

public class Blogger {
public static void main(String[] args) {
Random random = new Random();
System.out.println(random.nextInt(9));
}
}

This class will compile without any problem, and will print a random number between 0 and 9.

Another type of redundant import is when you import a class that is in the same package, for example,  suppose that you have a class "Comment.java" and "Blogger.java" in the package "mysite". it is not necessary to have the clause "import mysite.Comment" in the class "Blogger.java", as follows:

package mysite;

import java.util.Date;

import mysite.Comment; //Redundant import clause

public class Blogger {
public static void main(String[] args) {
Comment mycomment = new Comment("My comment for today", new Date());
System.out.println(mycomment.getCommnet());
}
}


package mysite;

import java.util.Date;

public class Comment {
private String comment;
private Date date;

public Comment(String comment, Date date){
this.comment = comment;
this.date = date;
}

public String getCommnet(){
return this.comment;
}

public Date getDate(){
return this.date;
}
}

this type of import as mentioned before it is not necessary since Java will automatically look for the classes that you are using in the same package.

So be careful with the way you import classes in java, even though many modern IDE can help you to automatically import package and classes, it is good to know and understand  this types of redundant imports because it can be a great help if you are facing an interview or solving a certification exam.

Recommended readings:
Reference: