i have the two classes :
public class ScriptTests {
private final java.nio.file.Path test_file;
// ...
}
public class TestD {
public static void main(String args[]) {
java.nio.file.Path test_file= Paths.get(args[0]);
// ....
}
}
Those classes are used to test code I wrote. My question is why did they always use this Package with its full name? Why not simply use import
at the start and then use a simpler name like they did in all the other Packages?
For example : import java.io.*;
I am asking because it just seemed weird. I think there is probably a reason!
You can use the entire declaration of the class using the package when you need to use two classes of different packages for example if you need to use both classes Date in one method. If you don't need to use two or more classes with the same name but with different packages is a good practice to use the import to become the code more readable.
Example
package com.stackoverflow.question;
import java.sql.Timestamp;
public class Solution {
public static void main(String args[]) {
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
java.util.Date utilDate = new java.util.Date();
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(sqlDate);
System.out.println(utilDate);
System.out.println(timestamp);
}
}
Note: It's a bad practice to import all the classes of one particular package ("import java.sql.*"), just import the classes that you need.