Packages in Java is one of the important concept. All Java classes and interfaces are arranged in package structure.
What is package in java
Packages is a namespace which contains related classes and Interfaces.
The methods and field of class define inside the package can be accessible in package outside package and in child classes based on access specifier.
Packages are stored in a hierarchical manner and explicitly imported into
Types of packages in java
Java packages are classified into two types
- Predefined or Built-in package
- User defined package
1 Java API(Application Programming Interface) packages or Predefined packages or Built-in packages
These packages are defined by the system. Some example of system defined packages
- java.lang: It contains fundamental classes like System, String Math, Number etc. This is the default package in java.
- java.util: This package contains collections framework, legacy collection classes, event model and other features.
- java.io: Provides system input output with stream classes and files
java .awt : Contain classes for creating user interface graphic and Image classes.- java.net: Contains classes related to networking and sockets
The Java 1.3 contains nearly 60 predefined packages
Importing packages in Java with Example
import java.util.Scanner; -To import Scanner class
import java.util.*; To import all classes in util package
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class ScannerTest{ public static void main(String [] s){ Scanner scanner=new Scanner(System.in); System.out.println("Enter your Name"); String name=scanner.nextLine(); System.out.println("Enter your Age"); int age=scanner.nextInt(); System.out.println("Hello From main "+name +" "+age); } } |
Here we import is used to import java.util.Scanner;
2 User defined packages in java
These packages are defined by the user.
How to create package in java
Defining as packages: To define a package, place “package” command as the first statement in the source file.
The syntax of packages creation :-
1 | package package_name; |
1 2 3 4 5 6 7 | mypackage; public class number { public void add(int a, int b) { System.out.println(" a + b =" + (a + b)); } } |
- The class that is defined in the package must be public. So that is can be accessible by any another of them.
- Java uses the file system file directories to store packages. We save the program with number.java and compile the package is as javac_d/number.java. Due to this compilation Mypackage directory is automatically created and .class file is stores in that directory.
Once the package creation has completed. Then the package information is including with the help of “import” keyword. import keyword links the package with our program.
Java Package Example
It
1 2 3 | import mypack.*; or import mypack.number; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package x; // package x public class Number // file name must be saver with class name(number.java) { public void add(int a, int b) { System.out.println(" sum =" + (a + b)); } } package y; import x.number; public class Test { public static void main(String a[]) { number ob = new number(); ob.add(10, 20); } } |
1 | sum = 30 |
- In this program, package is within x folder and main program is within y folder in c drive.
- Package name in this program is x.
- File name and package class name must be same ex: (number.java- source file).
How to compile package in java
c:\> javac folder/filename.java
c:\>java package.filename
1 2 3 4 | javac directory/filename c:\>javac x/number.java c:\>javac y/xyz.java c:\>java y.xyz |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package math1; // package within math1 folder public class Cal // file name must be saver with class name(Cal.java) { public void square(int a) { System.out.println(" Square =" + (a * a)); } public void cube(int a) { System.out.println(" Cube =" + (a * a * a)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package x; import math1.Cal; public class Test { public static void main(String a[]) { Cal ob = new Cal(); ob.square(10); ob.cube(10); } } |
1 2 | square = 100 cube = 1000 |
Note: If we want to access all the classes of information then the classes are stored in different files with the same package name.
Sub Packages
In Java, it is possible to create a sub packages for the main packages.
Syntax
package pack1.pack2;
1 | package pack1.pack2; |
Where pack1 is the main package and pack2 is the sub package.
Java packages sub package example
1 2 3 4 5 6 7 8 9 | package pack1; public class x { public void show() { System.out.println(" super"); } } |
1 2 3 4 5 6 7 | package pack1.pack2; public class y { public void display() { System.out.println("sub"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package z; import pack1.x; import pack1.pack2.y; public class Test { public static void main(String a[]) { x ob1 = new x(); ob1.show(); y ob2 = new y(); ob2.display(); } } |
Result
1 2 | super sub |
How many predefined packages in java
As per different java versions there are different packages and classes this SO question is also helpful.
We Inspected javase 14 and find there are 225 predefined packages / sub packages in java.
Which Package is default package in java
The package which is not required to import explicitly is known as default package.
If we write simple program to print hello then there we use System, String class we don’t need to import that. These are defined in java.lang
package.
Default Package in Java is java.lang
Which keyword is used for accessing the features of a package
The import keyword is used for accessing the features of a package.
We can import classes and interfaces in our current file using import.
What is Import in Java
Import in a keyword in java. We use import statement to import class or all classes from a package.
How to import a package in java
import java.util.Date;
orimport java.util.*;
How to create a package in java (Steps to create package in java with example)
- Create a folder with package name
- Create java files inside folder
- each java files include package definition
package packagename;
packages in java with example program
1 2 3 4 5 6 7 8 | package one; public class First{ public static void main(String[] s){ System.out.println("Hello From one.First"); } } |
1 2 3 | D:\>javac one/First.java D:\>java one.First Hello From one.First |
How to run package program in java (How to execute package in java)
compile d:\>javac foldername/filename.java
run d:\>java packagename.filename
What is java lang package in java
java lang (java.lang) is default package in Java. It contains fundamental classes.
Use of packages in java
- Group related classes and interfaces
- To avoid Naming conflicts
- To provide Access Control
Example of predefined packages in java
Predefined packages are defend by Java Team like
- java.lang (default package)
- java.util
- java.io
- java.net
- java.math
- java.sql
- java.time
- java.awt
- java.text
The package which should be created to categorize the package further
The package which should be created to categorize the package further is sub package
java package java.
sub packages java.land, java.util etc.
Which of the following is used to avoid naming conflicts?
- class
- Interface
- package
- templet
Answer is package