Packages is a way of grouping related classes, interfaces and enums. Using package, you can avoid clashing of names and control accessibility of classes and interfaces.
There are two types of packages in Java-
With the help of package statement, you can create a package. This statement consists of package keyword followed by the package name.
Package statement must be the first statement in a source code. Organisation must use reverse domain name to name their package. For example, if your domain name is tutorialsandyou.com, then package name will be com.tutorialsandyou.
The package represents folder on a file system. When you compile a program having a package statement in it, then the compiler creates a folder with the same name as that package name and places the generated .class file into that folder.
//Save as Square.java package mypackage1; public class Square { public void area(int side) { System.out.println("Area of square = "+side*side); } public static void main(String args[]) { Square s = new Square(); s.area(5); } }
Follow the below syntax to compile your program
javac -d DirectoryName filename.java
For example
javac -d . Square.java
-d option is used to specify the destination folder where the generated .class file will be placed.
Use the fully qualified name of .class file to run the program.
java mypackage1.Square
Output
Area of square = 25
The import statement is used to access classes and interface present in different package without using their fully qualified name. There are three different ways to access package from another package-
Note- The sequence of writing the program is package statement followed by one or more import statement then the class or interface declaration. If you change this sequence, you will get compile time error.
When you want to access a single class from another package, use import packagename.classname
Example
//Save as Square.java package mypackage1; public class Square { public void area(int side) { System.out.println("Area of square = "+side*side); } }
//Save as Test.java package mypackage2; import mypackage1.Square; class Test { public static void main(String args[]) { Square s = new Square(); s.area(5); } }
Output
Area of square = 25
When you want to access all the classes and interfaces present in another package, use import packagename.*
Example
//Save as Square.java package mypackage1; public class Square { public void area(int side) { System.out.println("Area of square = "+side*side); } }
//Save as Test.java package mypackage2; import mypackage1.*; class Test { public static void main(String args[]) { Square s = new Square(); s.area(5); } }
Output
Area of square = 25
Without using import statement, you can still access classes and interfaces stored in a separate package by using their fully qualified name. But this is not considered a good practice, and you should avoid using it.
Example
//Save as Square.java package mypackage1; public class Square { public void area(int side) { System.out.println("Area of square = "+side*side); } }
//Save as Test.java package mypackage2; class Test { public static void main(String args[]) { mypackage1.Square s = new mypackage1.Square(); s.area(5); } }
Output
Area of square = 25
To enable compiler and JRE, to find classes and interfaces of different package add classpath to javac and java command. There are two ways of adding classpath-
Using import static statement, you can import static members of a class or an interface. By using this statement, you can simply access static variables or methods by their name and there no need to prefix them with the class name.
import static java.lang.Math.PI; class Test { public static void main(String args[]) { System.out.println("The value of PI is "+PI); } }
Output
The value of PI is 3.1415926