Package Notes
Package Notes
A folder that is linked with java class is called package. It is used to group
classes with the same name, also we can create user defined classes with
Packages are used in Java, in-order to avoid name conflicts and to control
Using package it becomes easier to locate the related classes and it also
provides a good structure for projects with hundreds of classes and other
files.
Advantages of Packages:-
Java provides a large number of classes grouped into different packages based
on a particular functionality.
As the name suggests, these packages are defined by the user. We create a
directory whose name should be the same as the name of the package. Then
we create a class inside the directory.
Step1: Simply include the package statement that must be the first
statement in java source file. Any class declare with in that file belongs to
that specified package.
Step2:- Next define the class that is to be put in the package and declare
that class as public.
Step3: Now save the file the with name given to class and add .java
extension.
Sample.java
package mypack;
public class Sample
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
Compilation:-
Execution:-
Here current package in C:\test folder ,now to execute this program we need
to set class path.
The CLASSPATH refers to the path on your file system where your .class
files are saved, and the classpath is defined by the CLASSPATH
environment variable.
The CLASSPATH environment variable specifies the directories where you
want the compiler and the JVM to search for bytecode
1. import package.*;
2. import package.classname;
3. 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.
//save by B.java
package mypack;
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Output: Hello java
2) Using packagename.classname:
If you import package.classname then only declared class of this package will be
accessible.
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
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.
Output: Hello
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.
Subpackage in java:
Let's take an example, Sun Microsystem has definded a package named java that
contains many classes like System, String, Reader, Writer, Socket etc. These
classes represent a particular group e.g. Reader and Writer classes are for
Input/Output operation, Socket and ServerSocket classes are for networking etc and
so on. So, Sun has subcategorized the java package into subpackages such as lang,
net, io etc. and put the Input/Output related classes in io package, Server and
ServerSocket classes in net packages and so on.
package com.javatpoint.core;
class Simple{
public static void main(String args[]){
System.out.println("Hello subpackage");
}
}
To Compile: javac -d . Simple.java
There is a scenario, I want to put the class file of A.java source file in classes folder
of c: drive.
For example:
//save as Simple.java
package mypack;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
To Compile:
To Run:
To run this program from e:\source directory, you need to set classpath of the directory
The -classpath switch can be used with javac and java tool.
To run this program from e:\source directory, you can use -classpath switch of java
that tells where to look for class file. For example:
While using packages and inheritance in a program, we should be aware of the visibility
restrictions imposed by various access modifiers.
Access protection
Access Location
public class A
// body of A
The package p1 contains one public class by name A. Suppose we want to add another class B to
this package. This can be done as follows:
1. Define the class and make it public
2. Place the package statement
package p1;
package p1;
public class B
package p1;
{ public class B{
//body op B
4. Switch to the subdirectory created earlier and compile each source file. When completed,
the package would contain .class files of all the source files.
Since a Java source file can have only one declared as public, we cannot put two or more public
classes together in a .java file.
This is because of the restriction that the file name should be same as the name of the public
class with .java extension.
Hiding Classes
When we import a package using asterisk ( * ), all public classes are imported. However, we
may prefer to “not import” certain classes.
That is, we may like to hide these classes from accessing from outside of the package. Such
classes should be declared “not public”.
Example
package p1;
//body of x
}
class Y
}
Here, the class Y which is not declared public is hidden from outside of the package p1.
This class can be seen and used by other classes in the same package.
Now, consider the following code, which imports the package p1 that contains class X and Y:
Import p1.*;
Static import
As import statement allows to use a class without its package qualification, static import
allows to access the static members of a class without class qualifications. For Example, to
access the static methods you need to call the using class name −
Math.sqrt(169);
But, using static import you can access the static methods directly.
Example
importstaticjava.lang.Math.*;
publicclassSample{
publicstaticvoidmain(String args[]){
System.out.println(sqrt(169));
Static import in Java allows to import static members of class and use them, as they are
declared in the same class. Static import is introduced in Java 5 along with other features
like Generics, Enum, Autoboxing and Unboxing and variable argument methods.
In order to access static members, it is necessary to qualify references with the class they came from.