Jumat, 30 Mei 2014

What is static import in Java 5 with Example




Static import in Java allows to import static members of class and use
them, as they are declared in the same class. Static import is introduced in
Java 5 along with other features like Generics,
Enum,
Autoboxing
and Unboxing
and variable
argument methods
. Many programmer think that using static import can reduce
code size
and allow you to freely use static field of external class
without prefixing class name on that. For example without static import you will
access static constant
MAX_VALUE of Integer class as Integer.MAX_VALUE but by
using static import you can import
Integer.MAX_VALUE and refer
it as
MAX_VALUE. Similar to regular import
statements, static import also allows wildcard * to import all static
members
of a class. In next section we will see Java program to demonstrate
How to use static import statements to import static fields.






Static
import example in Java


What is static import in Java 5 with ExampleIn this static import example, we have imported constants Integer.MAX_VALUE and Integer.MIN_VALUE statically
and printing there value without prefixing class
name
on that.








package test;

import static
java.lang.Integer.MAX_VALUE;

import static
java.lang.Integer.MIN_VALUE;

/**

 *

 * Java program to demonstrate How to use static import in Java 5

 * By using static import you can use static field of external class

 * as they are declared in same class.

 *

 * @author Javin Paul

 */


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


        System.out.println("Maximum value of int variable using " +


                            static import :
"
+
MAX_VALUE);

        System.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 you look at import statements import static java.lang.Integer.MAX_VALUE, its
written as import static rather than static import, so just
beware of that. We are not using
* wildcard
here and importing only selected static member but you can also use
import
static java.lang.Integer.*
to import
all static
fields
in one go.





Advantages of Static Import in Java



Main advantage of using static
import
in Java is saving keystrokes. If you are frequently using
System.out.println() statements
and tried of typing it, you can static import
System.out or System.* and subsequently
you can type
out.println() in your code, Though I would
suggest to use this Eclipse
shortcut to generate System.out.println statement
which is much faster than
static import. This is the kind of usage I see one can benefit from static
import
, other than that static import is just extension of regular import
statement in Java. Similar to static field you can also import static
method
in your class, mostly in case of Utility classes.





Drawback
of Static Import in Java


Many Java programmer argue against static import with
reason that it reduces readability and goes against how static field
should be used i.e. prefixed with class name e.g.
Integer.MAX_VALUE. Static
import has another drawback in terms of conflicts, once you static
import
Integer.MAX_VALUE you can not use MAX_VALUE as
variable in your programmer, compiler will throw error. Similarly if you static
import both
Integer.MAX_VALUE and Long.MAX_VALUE and refer
them in code as
MAX_VALUE, you will get following compile
time
error :





java.lang.ExceptionInInitializerError


Caused by: java.lang.RuntimeException: Uncompilable source code -
MAX_VALUE is already defined in a static single-type import 


       
at test.StaticImportExample.(StaticImportExample.java:6)


       
Could not find the main class: test.StaticImportExample.  Program will exit.


       
Exception in thread "main" Java Re





Summary


Finally few points worth remembering about static import in Java :





1) Static import statements are written as "import
static"
in code and not "static import".





2) If you import two static fields with same name explicitly e.g. Integer.MAX_VALUE and Long.MAX_VALUE then Java
will throw compile time error. But if other static modifier is not imported
explicitly e.g. you have imported
java.lang.Long.*, MAX_VALUE will refer
to
Integer.MAX_VALUE.





3) Static import doesn't improve readability as expected, as many Java
programmer prefer
Integer.MAX_VALUE which is clear that which MAX_VALUE are you
referring.





4) You can apply static import statement not only
on static fields but also on static methods in Java.





That's all on What is static import in Java 5, What is advantages
and drawbacks of using static import in Java program and how to use static
import in Java. Honestly, its been almost a decade with Java 5 released but I
have rarely used
static import statements. May be in future I may
figure out a strong convincing reason to use static import





Other Java 5 tutorial from Javarevisited






























Source:http://javarevisited.blogspot.com/2012/10/what-is-static-import-in-java-5-example-tutorial.html

Tidak ada komentar:

Posting Komentar