OOPS [java] Unit 2 Package
OOPS [java] Unit 2 Package
• There are many built-in packages such as java, lang, awt, javax,
swing, net, io, util, sql etc.
Java Package
• Here, we will have the detailed learning of creating and using user-
defined packages.
Advantage of Java Package
JAVA PACKAGE
import java.awt.*;
import java.awt.Colour;
• The -d switch specifies the destination where to put the generated class file. You can use any
directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep
the package within the same directory, you can use . (dot).
How to run java package program
You 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 package
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.
Creating a Package
package firstpack; 1. Declare the pkg. at the beginning of the file.
2. Save the file with the same name as the class name.
public class Firstclass
{ 3. Save it in the directory having same name as the package
name and that directory should be the subdirectory of the
directory where all the source files are saved.
}
4. Compile the file. It will create .class file in the
class Secondclass subdirectory.
{
Java also support the concept of pkg. hierarchy.
Package p1.p2.p3;
}
It should be saved in directory p1/p2/p3.
class Thirdclass
{ Java pkg. file have more than one class definition. But only
one class will be declared as public. Compilation will leads to
independent .class files corresponding to each class.
}
Using user defined Package
package p1; import p1.ClassA;
class Pkgtest
public class ClassA
{
{
public static void main(String[] args)
public void displayA() {
{ ClassA ob = new ClassA();
ob.displayA();
System.out.println(“Class A”);
}
}
}
}
How to access package from another
package?
There are three ways to access the package from
outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
1) Using packagename.*
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 B.java
package pack; package mypack;
public class A{ import pack.*;
public void msg() class B{
{System.out.println("Hello");} public static void main(String args[]){
} A obj = new A();
//save by A.java obj.msg();
}
}
Output:Hello
2.Using packagename.classname
2) Using packagename.classname //save by B.java
If you import package.classname then package mypack;
only declared class of this package will import pack.A;
be accessible.
class B{
//save by A.java
public static void main(String args[]){
A obj = new A();
package pack;
obj.msg();
public class A{
}
public void msg()
}
{System.out.println("Hello");}
Output:Hello
}
3.Using fully qualified name
3 Using fully qualified name //save by A.java
• If you use fully qualified name then only
declared class of this package will be package pack;
accessible. Now there is no need to import.
But you need to use fully qualified name every public class A{
time when you are accessing the class or public void
interface.
msg(){System.out.println("Hello")
;}
}
Example of package by import fully qualified