A java package is a group of similar types of classes,
interfaces and sub-packages.
Package in java can be
categorized in two form, built-in package and user-defined package.
1) Java package is used to
categorize the classes and interfaces so that they can be easily maintained.
2) Java package provides access
protection.
The package keyword is used to create a
package in java.
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to java
packages");
}
}
How to compile java
package?
If
you are not using any IDE, you need to follow the syntax given
below:
javac -d directory javafilename
For ex: javac -d . Simple.java
How to run java package
program?
We need to use fully qualified name e.g. mypack.Simple
etc to run the class.
To Compile: javac
-d . Simple.java
|
To Run: java
mypack.Simple
Output:Welcome to java packages
|
The -d is a switch that
tells the compiler where to put the class file i.e. it represents destination.
The . represents the current folder.
How to access package from another package?
There
are three ways to access the package from outside the package.
- import
package.*;
- import
package.classname;
- fully
qualified name.
1) Using packagename.*
If
you use package.* then all the classes and interfaces of this package will be
accessible but not subpackages.
The
import keyword is used to make the classes and interface of another package
accessible to the current package.
Example of package that import the packagename.*
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
public class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
2) Using packagename.classname
If
you import package.classname then only declared class of this package will be
accessible.
Example of package by import package.classname
//save by A.java
package pack;
public class A{
public void
msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static
void main(String args[]){
A obj = new
A();
obj.msg();
}
}
Output:Hello
3) Using fully qualified name
If
you use fully qualified name then only declared class of this package will be accessible.
Now there is no need to import. But you need to use fully qualified name every
time when you are accessing the class or interface.
It
is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.
Example of package by import fully qualified name
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
Output:Hello
Note: If you import a package, subpackages
will not be imported.
If
you import a package, all the classes and interface of that package will be
imported excluding the classes and interfaces of the subpackages. Hence, you
need to import the subpackage as well.
save as E.java
package com.mypack1;
public class E {
public void methodE(){
System.out.println("from E");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
save as X.java
package com.mypack1;
public class X {
public void methodX(){
System.out.println("from X");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
//save by F.java
package com.mypack1.subpack1;
public class F {
public void methodF(){
System.out.println("from F");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
//save by Y.java
package com.mypack2;
import com.mypack1.*;
import com.mypack1.subpack1.F;
public class Y {
public void methodY(){
System.out.println("from Y");
}
public static void main(String[] args) {
X x1 = new X();
x1.methodX();
F f1 = new F();
}
}
save as E.java
package com.mypack1;
public class E {
public void methodE(){
System.out.println("from E");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
save as X.java
package com.mypack1;
public class X {
public void methodX(){
System.out.println("from X");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
//save by F.java
package com.mypack1.subpack1;
public class F {
public void methodF(){
System.out.println("from F");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
//save by Y.java
package com.mypack2;
import com.mypack1.*;
import com.mypack1.subpack1.F;
public class Y {
public void methodY(){
System.out.println("from Y");
}
public static void main(String[] args) {
X x1 = new X();
x1.methodX();
F f1 = new F();
}
}
How to send the class file to another directory or drive?
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
To Compile:
e:\sources>
javac -d c:\classes Simple.java
To Run:
To run this program from e:\source directory, you
need to set classpath of the directory where the class file resides.
|
e:\sources> set
classpath=c:\classes;.;
|
e:\sources> java mypack.Simple
|
How to put two public classes in a package?
If you want to put two public classes in a package,
have two java source files containing one public class, but keep the package
name same. For example:
|
//save as A.java
package java9m;
public class A{}
//save as B.java
package java9m;
public class B{}
package class in java
The package class provides methods to get information
about the specification and implementation of a package. It provides methods
such as getName(), getImplementationTitle(), getImplementationVendor(),
getImplementationVersion() etc.
Example of Package class
In this example, we are printing the details of
java.lang package by invoking the methods of package class.
public class
PackageInfo{
public static
void main(String args[]){
Package
p=Package.getPackage("java.lang");
System.out.println("package name:
"+p.getName());
System.out.println("Specification Title:
"+p.getSpecificationTitle());
System.out.println("Specification Vendor:
"+p.getSpecificationVendor());
System.out.println("Specification
Version: "+p.getSpecificationVersion());
System.out.println("Implementaion Title:
"+p.getImplementationTitle());
System.out.println("Implementation
Vendor: "+p.getImplementationVendor());
System.out.println("Implementation Version:
"+p.getImplementationVersion());
System.out.println("Is sealed:
"+p.isSealed());
}
}
Output:
package
name: java.lang
Specification
Title: Java Platform API Specification
Specification
Vendor: Oracle Corporation
Specification
Version: 1.8
Implementaion
Title: Java Runtime Environment
Implementation
Vendor: Oracle Corporation
Implementation
Version: 1.8.0_102
Is sealed: false
No comments:
Post a Comment