0% found this document useful (0 votes)
4 views

Java Packages

Java packages are a way to group related classes, interfaces, and sub-packages, helping to prevent naming conflicts and organize code. They can be built-in or user-defined, with built-in packages like java.lang and java.util providing essential classes. Users can import specific classes or entire packages, and static imports allow for easier access to static members without class qualification.

Uploaded by

pvy1524
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Packages

Java packages are a way to group related classes, interfaces, and sub-packages, helping to prevent naming conflicts and organize code. They can be built-in or user-defined, with built-in packages like java.lang and java.util providing essential classes. Users can import specific classes or entire packages, and static imports allow for easier access to static members without class qualification.

Uploaded by

pvy1524
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Packages

Packages in Java are a mechanism that encapsulates a group of classes, sub-


packages, and interfaces. Packages are used for:
 Prevent naming conflicts by allowing classes with the same name to exist in
different packages,
like college.staff.cse.Employee and college.staff.ee.Employee.
 They make it easier to organize, locate, and use classes, interfaces, and other
components.
 Packages also provide controlled access for Protected members that are
accessible within the same package and by subclasses.
 Also for default members (no access specifier) that are accessible only within
the same package.
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.

Working of Java Packages

Directory 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:
 college.tech.cse
 college.tech.ee
 college.art.history

Example:

import java.util.*;

Here, util is a sub-package created inside java package.

Accessing Classes Inside a Package


In Java, you 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.

Types of Java Packages

 Built-in Packages
 User-defined Packages

1. Built-in Packages

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:

 java.lang: Contains language support classes(e.g classes which defines


primitive data types, math operations). This package is automatically imported.

 java.io: Contains classes for supporting input / output operations.

 java.util: Contains utility classes which implement data structures like Linked
List, Dictionary and support ; for Date / Time operations.

 java.applet: Contains classes for creating Applets.

 java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc). 6)

 java.net: Contain classes for supporting networking operations.

User-defined Packages
These are the packages that are defined by the user. 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
// Name of the package must be same as the directory
// under which this file is saved
package myPackage;
public class MyClass{
public void getNames(String s)
{
System.out.println(s);
}
}

Now we will use the MyClass class in our program.


// import 'MyClass' class from 'names' myPackage
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);
}
}
Using Static Import

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.

// Note static keyword after import.


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:

java.util.Date deadLine = new java.util.Date();


java.sql.Date today = new java.sql.Date();

Illustration of user-defined packages

Creating our first package: File name – ClassOne.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
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

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();
}
}

You might also like