Java – Divide the Classes into Packages with Examples
Last Updated :
29 Oct, 2021
Let us first know what is a class and package in Java. Class in java is a model for creating objects. It means that the properties and actions of the objects are written in class. Properties are represented by variables and actions of the objects are represented by methods. So, a class contains variables and methods. The same variables are also available in the objects because they are created from the class. These variables are also called ‘instance variables’ because they are created inside the object (instance).
Illustration:
class Human
{
// Properties --> Instance variables
String name;
int age;
// Actions --> methods
void speak()
{
System.out.println("Hey there! I am " + name);
System.out.println("My age is " + age);
}
}
Output:
Hey there! I am Vasanth Kumar
My age is 20
Note: Here the keyword class is used to declare a class. After this, we should write a class name. In the class, we write instance variables and methods.
Packages in Java
In Java, Packages are a collection of classes, sub-packages, and interfaces. i.e A package represents a dictionary that contains a related group of classes and interfaces. Whenever we write a statement be it as shown below we are importing all the classes of java.io.* package. Here, java is a directory name and io is another sub directory within it. And the * represents all the classes and interfaces of that io subdirector.
import java.io.*;
We have many such packages, for example, java.lang, java.util, and there do exist many more inside which their d lies classes inside the package. In order to use so let us do take the most frequently used packages that are the ‘lang’ package for java designing and the ‘io’ package for input-output operations.
Let us discuss advantages of packages in java hat are as follows:
- Packages are useful to arrange related classes to interfaces into a group. This puts together all the classes and interfaces performing the same task in the package. For example, In Java, all the classes and interfaces which perform input and output operations are stored in the java.io package.
- Packages hide the classes and interfaces in a separate subdirectory.
- The classes and interfaces of a package are isolated from the classes and interfaces of another package.
Types of packages
There are two types of packages in java as listed:
- Built-in packages
- User-defined packages
Type 1: Built-in Packages
These are the packages that are already available in the Java language. These packages provide nearly all necessary classes, interfaces, and methods for the programmer to perform any task. They are as follows:
- java.lang
- java.util
- java.io
- java.awt
- javax.swing
- java.net
- java.applet
- java.text
- java.sql
Type 2: User-defined Packages
Just like the built-in packages shown earlier, the user of the java language can also create their own packages. They are called User Defined Packages. These packages can also be imported into other classes and used exactly in the same way as built-in packages.
Syntax: Creating a package, package keyword is used
2.1.1 To create a package
package package_name ;
2.1.2 To create a subpackage within a package
package package_name.subpackagename ;
2.2 Compile
C:\> javac -d . classname.java
2.3 To run the program
C:\> java packagename.classname
Above syntax is valid only for windows CMD shell, fo rlinux and mac zsh shell refer to below media as perceive it the same way provided below

Implementation:
Now let us divide the class into packages
Step 1: Define a class StudentData that holds the following information of a student:
- ID: It is a string that stores the unique ID of each student
- Name: It is a string indicating the name of the student.
Note that these fields should be declared private.
Step 2: Create another class StudentDataExtended with a private attribute named location ( a String that stores the location of the student).
Step 3: Now in this class define a method addDetails() method that stores the details of students; and a method printDetails() method that outputs the details of students in the sorted order of their id.
Note : Both the classes should be in different packages
Illustration:
Enter the number of students : 2
Enter the details of Student 1 (id, name, location):
B200 Ajay Hyderabad
Enter the details of Student 2 (id, name, location):
B100 Ramesh Hyderabad
The Student Details are:
B100 Ramesh Hyderabad
B200 Ajay Hyderabad
Example
Java
package pack1;
public class StudentData {
private String id;
private String name;
public void addStudentData(String id, String name)
{
this .id = id;
this .name = name;
}
public String getId() { return id; }
public String getName() { return name; }
}
|
Java
package pack2;
import java.io.*;
import java.lang.*;
import java.util.*;
import pack1.*;
class StudentDataExtended extends StudentData {
private String location;
public void addDetails(String id, String name,
String location)
{
addStudentData(id, name);
this .location = location;
}
public static void
printDetails(TreeMap<String, StudentDataExtended> map)
{
for (String a : map.keySet()) {
StudentDataExtended student = map.get(a);
System.out.println(student.getId() + " "
+ student.getName() + " "
+ student.location);
}
}
public static void main(String[] args)
{
System.out.print( "Enter the number of students : " );
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String buffer = sc.nextLine();
int count = 1 ;
TreeMap<String, StudentDataExtended> map
= new TreeMap<>();
while (n != 0 ) {
System.out.println(
"Enter the details of Student " + count
+ " (id, name, location):" );
count++;
String details = sc.nextLine();
String studentInfo[] = details.split( " " );
StudentDataExtended student
= new StudentDataExtended();
student.addDetails(studentInfo[ 0 ],
studentInfo[ 1 ],
studentInfo[ 2 ]);
map.put(studentInfo[ 0 ], student);
n--;
}
System.out.println( "\nThe Student Details are:" );
printDetails(map);
}
}
|
Output:

Similar Reads
Throwable Class in Java with Examples
Classes and Objects are basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In this article,
7 min read
Modifiers classModifiers() method in Java with Examples
The classModifiers() method of java.lang.reflect.Modifier class is used to get an integer value together with the modifiers of source language that can be applied to a class. Syntax: public static boolean classModifiers() Parameters: This method accepts nothing. Return: This method returns an int va
1 min read
Open Closed Principle in Java with Examples
In software development, the use of object-oriented design is crucial. It helps to write flexible, scalable, and reusable code. It is recommended that the developers follow SOLID principles when writing a code. One of the five SOLID principles is the open/closed principle. The principle states that
9 min read
Class forName() method in Java with Examples
The forName() method of java.lang.Class class is used to get the instance of this Class with the specified class name. This class name is specified as the string parameter.Syntax: public static Class<T> forName(String className) throws ClassNotFoundException Parameter: This method accepts the
1 min read
Class getName() method in Java with Examples
The getName() method of java.lang.Class class is used to get the name of this entity. This entity can be a class, an array, an interface, etc. The method returns the name of the entity as a String.Syntax: public String getName() Parameter: This method does not accept any parameter.Return Value: This
1 min read
Class getPackage() method in Java with Examples
The getPackage() method of java.lang.Class class is used to get the package of this entity. This entity can be a class, an array, an interface, etc. The method returns the package of this entity.Syntax: public Package getPackage() Parameter: This method does not accept any parameter.Return Value: Th
1 min read
Class getClasses() method in Java with Examples
The getClasses() method of java.lang.Class class is used to get the classes of this class, which are the class and interfaces that are public and its members. The method returns the classes of this class in the form of array of Class objects. Syntax: public Class[] getClasses() Parameter: This metho
2 min read
Java class dependency analyzer in Java 8 with Examples
Java class dependency analyzer: jdeps is a new command-line tool introduced in JDK 8 to understand the static dependencies and libraries of application i.e. jdeps command shows the package-level or class-level dependencies of Java class files. The input for jdeps can be a .class file pathname, a JAR
2 min read
Package toString() method in Java with Examples
The toString() method of java.lang.Package class is used to get the String representation of this package instance. The method returns the String representation as a String value. Syntax: public String toString() Parameter: This method does not accepts any parameter. Return Value: This method return
1 min read
Class getModule() method in Java with Examples
The getModule() method of java.lang.Class class is used to get the module of this entity. This entity can be a class, an array, an interface, etc. The method returns the module of the entity.Syntax: public Module getModule() Parameter: This method does not accept any parameter.Return Value: This met
2 min read