Skip to main content

Use of Static Imports in Java with Example


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.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 " +
static import : " + Integer.MIN_VALUE);
     
        
//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" +
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

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

Popular posts from this blog

Java Interview Programs

Strings: 1.) Print Duplicates of an array with one Loop statement? package com.java.testing; import java.util.HashSet; import java.util.Set; class MyClass { public static Set<Integer> commonNumbers = new HashSet<>(); public Integer digits; public MyClass(Integer digits) { this.digits=digits; } @Override public int hashCode() { return 0; } @Override public boolean equals(Object obj) { if(obj instanceof MyClass) { if(this.digits.equals(((MyClass)obj).digits)) { commonNumbers.add(((MyClass)obj).digits); return true; } } return false; } } public class ObenStringQuestion { public static void main(String[] args){ int[] mobileno = new int[]{1,8,8,8,8,8,8,1,4,2,8,2,1,1,6,2}; Set<MyClass> xx = new HashSet<>(); for(int i=0;i<mobileno.length;i++) { xx.add(new MyClass(mobileno[i])); } for (MyClass object : xx) { System.out.println(object.digits);...

Shell Scripting - Best Practices

Best Practices   Shell Scripting  : Most programming languages have a set of "best practices" that should be followed when writing code in that language. However, I have not been able to find a comprehensive one for shell scripting so have decided to write my own based on my experience writing shell scripts over the years. A note on portability : Since I mainly write shell scripts to run on systems which have Bash 4.2 installed, I don't need to worry about portability much, but you might need to! The list below is written with Bash 4.2 (and other modern shells) in mind. If you are writing a portable script, some points will not apply. Needless to say, you should perform sufficient testing after making any changes based on this list :-) Here is my list of best practices for shell scripting (in no particular order): Use functions Document your functions Use  shift  to read function arguments Declare your variables Quote all parameter expansions...