Packages in Java are a mechanism that encapsulates a group of classes, sub-packages and interfaces. Packages are used for:
By grouping related classes into packages, Java promotes data encapsulation, making code reusable and easier to manage. Simply import the desired class from a package to use it in your program.
Creating Custom PackagesStep 1: Create a directory in which we create our packages and Java files.
mkdir PROGRAMMING
Step 2: Now, change the directory and create another folder inside the main folder
cd PROGRAMMING
mkdir JavaProgramming
cd JavaProgramming
mkdir arrays
Step 3: Now create an empty text file and write the below Java code and don't forget to save it with the same name as the class with .java extension (TwoPointers.java)
TwoPointers Class.
Java
package JavaProgramming.arrays;
// Main class present inside the package
public class TwoPointers {
public static void main(String[] args) {
System.out.println("Inside the package");
}
}
Note: Do not forget to add the package name inside the program file.
Step 4: Now run the program with the define folder path
javac src\JavaProgramming\arrays\TwoPointers.java
java src\JavaProgramming\arrays\TwoPointers.java
Output:
Runing program with Folder pathFolder Structure:
This is the visual representation of a custom package in Java in the below image. First, we create a folder named Progamming and inside it we create a package Javaprogramming and then create another subpackage, which is called arrays. Then, we create a Java class file inside it, which is shown in the image below:
Folder Structure Working of Java PackagesDirectory Structure: Package names and directory structures are closely related. For example, if a package name is college.staff.cse, then three directories are, college, staff and cse, where cse is inside staff and staff is inside the college.
Naming Conventions: Package names are written in reverse order of domain names, e.g., org.geeksforgeeks.practice. In a college, the convention might be:
Example:
import java.util.*;
Here, util is a sub-package created inside the java package.
Accessing Classes Inside a PackageIn Java, we can import classes from a package using either of the following methods:
1. Import a specific class:
import java.util.Vector;
This imports only the Vector class from the java.util package.
2. Import all classes from a package:
import java.util.*;
This imports all classes and interfaces from the java.util package but does not include sub-packages.
Example: Import the Vector class
Java
import java.util.Vector;
public class Geeks {
public Geeks() {
// java.util.Vector is imported, We are able to access it directly in our code.
Vector v = new Vector();
java.util.ArrayList l = new java.util.ArrayList();
l.add(3);
l.add(5);
l.add(7);
System.out.println(l);
}
public static void main(String[] args) {
new Geeks();
}
}
Note:
Types of Java Packagesimport java.util.Date;
import my.package.Date;
These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-in packages are:
These are the packages that are defined by the user.
1. Create the Package:
First we create a directory myPackage (name should be same as the name of the package). Then create the MyClass inside the directory with the first statement being the package names.
Example:
Java
package myPackage;
public class MyClass
{
public void getNames(String s)
{
System.out.println(s);
}
}
2. Use the Class in Program:
Now we will use the MyClass class in our program.
Java
import myPackage.MyClass;
public class Geeks {
public static void main(String args[]) {
// Initializing the String variable with a value
String s = "GeeksforGeeks";
// Creating an instance of class MyClass in the package.
MyClass o = new MyClass();
o.getNames(s);
}
}
Note: MyClass.java must be saved inside the myPackage directory since it is a part of the package.
Static Import in Java is about simplifying access to static members and separates it from the broader discussion of user-defined packages.
Static import is a feature introduced in Java programming language (versions 5 and above) that allows members (fields and methods) defined in a class as public static to be used in Java code without specifying the class in which the field is defined.
Example:
Java
import static java.lang.System.*;
class Geeks {
public static void main(String args[]) {
// We don't need to use 'System.out' as imported using static.
out.println("GeeksforGeeks");
}
}
Handling Name Conflicts
When two packages contain a class with the same name (e.g., java.util.Date and java.sql.Date), specify the full package name to avoid conflicts.
import java.util.*;
import java.sql.*;
//And then use Date class, then we will get a compile-time error :
Date today ; //ERROR-- java.util.Date or java.sql.Date?
The compiler will not be able to figure out which Date class do we want. This problem can be solved by using a specific import statement:
import java.util.Date;
import java.sql.*;
If we need both Date classes then, we need to use a full package name every time we declare a new object of that class. For Example:
Directory Structure and CLASSPATHjava.util.Date deadLine = new java.util.Date();
java.sql.Date today = new java.sql.Date();
Package names correspond to a directory structure. For example, a class Circle in package com.zzz.project1.subproject2 is stored as:
$BASE_DIR/com/zzz/project1/subproject2/Circle.class
CLASSPATH can be set by any of the following ways:
Go to Control Panel -> System -> Advanced -> Environment Variables.
.c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar
.
) represents the current working directory.> SET CLASSPATH
CLASSPATH can be set temporarily for that particular CMD shell session by issuing the following command:
> SET CLASSPATH=.;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar
Instead of using the CLASSPATH environment variable, you can also use the command-line option -classpath or -cp of the javac and java commands, for example,
> java –classpath c:\javaproject\classes com.abc.project1.subproject2.MyClass3
Illustration of user-defined packages: Creating our first package: File name – ClassOne.java
Java
package package_name;
public class ClassOne {
public void methodClassOne()
{
System.out.println("Hello there its ClassOne");
}
}
Creating our second package: File name – ClassTwo.java
Java
package package_one;
public class ClassTwo {
public void methodClassTwo()
{
System.out.println("Hello there i am ClassTwo");
}
}
Making use of both the created packages: File name – Testing.java
Java
import package_name.ClassOne;
import package_one.ClassTwo;
public class Testing {
public static void main(String[] args)
{
ClassTwo a = new ClassTwo();
ClassOne b = new ClassOne();
a.methodClassTwo();
b.methodClassOne();
}
}
Now having a look at the directory structure of both the packages and the testing class file:
Access Modifiers in the Context of Packagespublic
modifier are accessible from anywhere, regardless of whether the accessing class is in the same package or not.private
modifier are accessible only within the same class. They cannot be accessed by classes in the same package, subclasses, or different packages.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4