The main
reason behind the static import feature in java5 is to reduce the unnecessary reference
of class name to call static methods/fields.
package import.static.test;
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.MIN_VALUE;
import static java.lang.Integer.MIN_VALUE;
import static java.lang.System.out;
public class StaticImportExample {
public static void main(String args[]) {
//without Static import
System.out.println("Maximum value of int variable in Java without " +
"static import :
" + Integer.MAX_VALUE);
System.out.println("Minimum value of int variable in Java without " +
System.out.println("Minimum value of int variable in Java without " +
static import : " + Integer.MIN_VALUE);
//after static import in Java 5
out.println("Maximum value of int variable using " +
//after static import in Java 5
out.println("Maximum value of int variable using " +
static import : " + MAX_VALUE);
out.println("Minimum value of int variable using" +
out.println("Minimum value of int variable using" +
static import : " + MIN_VALUE);
}
}
Output:
Maximum value of int variable in Java without static import : 2147483647
Minimum value of int variable in Java without static import : -2147483648
Maximum value of int variable using static import : 2147483647
Minimum value of int variable using static import : -2147483648
}
}
Output:
Maximum value of int variable in Java without static import : 2147483647
Minimum value of int variable in Java without static import : -2147483648
Maximum value of int variable using static import : 2147483647
Minimum value of int variable using static import : -2147483648
If the other static imported
class has same method/field names.
1.)If it is wildcard * import,
then first methods/fields will considered from first import.
2)If it method/field specific
import, then java will throw a compile time error as,
In this case, we have to use
complete class name to use second static field with wildcard * imports.

Comments
Post a Comment
thanks