<-Pre | Home | Next-> |
What is Package?
A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to simply as classes and interfaces.
How to create package.
Suppose we have a file called Test.java, and we want to put this file in a package Hello. First thing we have to do is to specify the keyword package with the name of the package we want to use (Hello in our case) on top of our source file, before the code that defines the real classes in the package, as shown in our Test class below:
package Hello;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Can I import same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or same class multiple times. Neither compiler nor JVM complains anything about it. JVM will internally load the class only once no matter how many times you have imported the same class..Does importing a package imports the sub packages as well? E.g. Does importing java.util.* also import java.util.code.*? *IMP*
No. you have to import the sub packages explicitly. Importing java.util.* will import classes in the package util only. It will not import any class in any of its sub package’s.
Pre->abstract-class-interface | Next->garbage-collection |
*****Please feel free to give feedback at shrawan.iitg@gmail.com and comments below *****
No comments:
Post a Comment