Sunday, 11 September 2011
static import
Used when you want to use class static members.
1. import static java.lang.System.out;/*imports the static instance “out”.*/
now u can use “out” as:
out.println(“My name is Milan”);
2. import static java.lang.Integer.*; /*imports all the static method and variable of class Integer*/
now u can use static member of Interger as:
System.out.println(MAX_VALUE);
System.out.println(toHexString(4248));
3. import static java.lang.System.*;/*imports all the static methods and var of class System*/
Rules
1.import static java.lang.System.out.*;/*Compiler error. U cant do static imports on static reference,var or methods*/
2.If u do static imports on both Integer and Long class.Both class has Static var MAX_VALUE so there will be compiler error
3.static import java.lang.Integer.*; /*Compiler error cannot write “static import” instead of “import static”*/
Disadvantage
Program becomes less readable.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment