Classes present in java.lang package like String,System,Exception etc. are automatically imported in java program so that Programmer don't need to mention explicit class path in program like java.lang.System.out.println("Hello") and compiler can resolve the class name from the specified packages and compile the source code.
Some of the facts about java imports are -
1.java.lang package imported automatically(though not statically) i.e. programmer don't need to write java.lang.System.out.println("Hello") in order to print message instead s/he can write System.out.println("Hello").
2.Classes belong to the same package doesn't require explicit import.
3.Compiler resolves the class name at compile time , so error like "cannot resolved to a type" ,"Cannot find a symbol" you will get at compile time when classes you are using are not imported in your program.
4.You can import individual class , or all classes belong to the package or static members of class in single import statement.
e.g.
import java.util.ArrayList;
import java.util.*;
import static java.lang.System.out;
5.import statement are in the form of package.subpackage.class or package.subpackage.* or static package.subpackage.class.member.
e.g.java.util.List; //here java is package,util is subpackage and List is the class name
Now the question comes as what is the difference between import and static import ?
import statements imports class or classes within a package while static import which is a new feature in java 5 allows to import static members and functions of the class.
static import allows you to write less code and used when constants declared in another class need to be included in your program and where your class require to access static members of classes which can't be inherited or which are final.
e.g. Math class in java.lang package , Math class is final so you cannot inherit it , but static members can be imported using this new feature
import static java.lang.Math.PI;
public class JavaStaticImportTest
{
public static void main(String ar[])
{
System.out.println(getPiValue());
}
public static double getPiValue()
{
return PI;
}
}
Too much use of static import can pollute the source code and harmful for readability.
Does enmass class import impacts performance ?
Actually No , because classes are resolved at compile time , so it hardly matters when either one class or multiple classes compiled at compile time.However it improves readabilty and maintainability.
This again depends on individual developers perception, as for some java.util.List makes code more readable and for some java.util.* is much more readable.
Some of the facts about java imports are -
1.java.lang package imported automatically(though not statically) i.e. programmer don't need to write java.lang.System.out.println("Hello") in order to print message instead s/he can write System.out.println("Hello").
2.Classes belong to the same package doesn't require explicit import.
3.Compiler resolves the class name at compile time , so error like "cannot resolved to a type" ,"Cannot find a symbol" you will get at compile time when classes you are using are not imported in your program.
4.You can import individual class , or all classes belong to the package or static members of class in single import statement.
e.g.
import java.util.ArrayList;
import java.util.*;
import static java.lang.System.out;
5.import statement are in the form of package.subpackage.class or package.subpackage.* or static package.subpackage.class.member.
e.g.java.util.List; //here java is package,util is subpackage and List is the class name
Now the question comes as what is the difference between import and static import ?
import statements imports class or classes within a package while static import which is a new feature in java 5 allows to import static members and functions of the class.
static import allows you to write less code and used when constants declared in another class need to be included in your program and where your class require to access static members of classes which can't be inherited or which are final.
e.g. Math class in java.lang package , Math class is final so you cannot inherit it , but static members can be imported using this new feature
import static java.lang.Math.PI;
public class JavaStaticImportTest
{
public static void main(String ar[])
{
System.out.println(getPiValue());
}
public static double getPiValue()
{
return PI;
}
}
Does enmass class import impacts performance ?
Actually No , because classes are resolved at compile time , so it hardly matters when either one class or multiple classes compiled at compile time.However it improves readabilty and maintainability.
This again depends on individual developers perception, as for some java.util.List makes code more readable and for some java.util.* is much more readable.
No comments:
Post a Comment