Packages in Java
Defining,Creating & Accessing
Packages, Importing Packages,
and Programs.
Definition
Creating
Importing
Programs
Quiz
time
Follow
2.
Introduction to Packagesin Java
Imagine you are organizing a large library. The library has thousands of books on various subjects like Science,
Literature, Technology, History, etc. If all the books are placed randomly without any order, it would be challenging to
find a particular book when needed. To solve this problem, you would organize the books into sections and subsections.
For example:
• Science
• Physics
• Chemistry
• Biology
• Literature
• Fiction
• Non-Fiction
• Poetry
3.
How This Relatesto Java Packages
In Java, packages are used to group related classes and interfaces together, just like how books are grouped into sections
in a library. This makes it easier to manage and locate classes within large projects. Here’s how it works:
Organization: Packages help organize your classes and interfaces in a logical manner.
For example, you might have packages for different layers of your application like com.myapp.models,
com.myapp.services, and com.myapp.controllers.
Avoid Name Conflicts: By placing classes in different packages, you can avoid name conflicts.
For example, you can have a User class in com.myapp.models and another User class in com.otherapp.models.
Access Control: Packages also help in controlling access to classes and members.
For instance, you can make classes and members package-private, so they are only accessible within the same package.
Reusability: Promotes code reuse by grouping related classes and interfaces that can be easily imported and used in other
programs.
4.
Definition
Creating
Importing
Programs
Quiz
Time
Follow
Definition
A javapackage is a group of similar types of classes, interfaces and
sub-packages. (or)
A package in Java is a namespace that organizes a set of related classes
and interfaces.
Packages act as containers for classes, interfaces, and sub-packages.
5.
Advantages of JavaPackage
1. Java package is used to categorize the classes and interfaces so that they can be
easily maintained.
2. Java package provides access protection.
3. Java package removes naming collision.
6.
Types Of Packagesin Java
There are basically 2 types of java packages.
They are as follow :
I. System Packages or Java API
II. User Defined Packages
7.
System Defined Packages
System packages, also known as Java API packages, are pre-defined packages provided by the Java
Development Kit (JDK).
They contain a wide range of classes and interfaces that are essential for Java programming.
Common Java API Packages:
java.lang - Contains classes for primitive types, strings, mathfunctions, threads, and exception
java.util - Contains classes such as vectors, hash tables, date etc.
java.io - Stream classes for I/O
User Defined Packages
User defined packages are created by developers to group related classes and interfaces that they define.
They help in organizing the codebase and avoiding naming conflicts.
• Benefits of User-Defined Packages
1.Customization: Users can create packages that suit their specific needs and preferences.
2.Organization: Helps in keeping similar classes together, making the project easier to manage.
3.Conflict Prevention: Reduces the chances of having classes with the same name causing confusion.
4.Reuse: Encourages the use of the same code in different parts of the project or in different projects.
5.Teamwork: Makes it easier for multiple people to work on the same project without interference.
10.
Definition
creating
Importing
Programs
Quiz
Time
follow
Choose aPackage Name: Decide on a meaningful name for your package that
reflects its content. Syntax: package packagename.
Create a directory structure that matches your package name.
Example: Create directories com/example/myapp.
Add the package Statement at the top of your Java file, add the package
statement.
package com.example.myapp;
Save your Java file in the corresponding directory.
Example: Save MyClass.java in com/example/myapp.
Compile Your Java File: Use the javac command to compile your Java file.
Example: javac com/example/myapp/MyClass.java
In other Java files, use the import statement to use classes from your package.
import com.example.myapp.MyClass;
STEPS FOR CREATING PACKAGE :
11.
Rules for Declaringa Package
• Package Declaration at the Top: The package statement must be the first line in the Java source
file, before any import statements or class declarations.
• Use Lowercase Letters: Package names should be written in all lowercase letters to avoid
conflicts with class names.
• Avoid Reserved Keywords: Do not use Java reserved keywords as package names.
Example: int, class, void, etc.
• No Special Characters: Package names should not contain special characters or spaces. Use
letters and numbers only.
Example: com.example.myapp is valid; com.example.my-app is not.
• Meaningful Names: Choose meaningful names that clearly indicate the purpose of the package.
Example: com.example.utils for utility classes, com.example.models for data models.
12.
Example of packageDeclaration
package p1;
public class C1{
public static void main(String args[]){
System.out.println("package creation");
}
}
How to compile java package
• javac -d directory javafilename
• Ex:javac -d . C1.java (The -d switch specifies the destination where to put the generated class
file)
How to run java package
• java directory.javafilename
• Ex:java p1.C1
1) Using packagename.*
•If you use package.* then all the classes and interfaces of this package
will be accessible but not subpackages.
• The import keyword is used to make the classes and interface of
another package accessible to the current package
3)Using fully qualifiedname
• If you use fully qualified name then only declared class of this package
will be accessible. Now there is no need to import. But you need to
use fully qualified name every time when you are accessing the class
or interface.
• It is generally used when two packages have same class name e.g.
java.util and java.sql packages contain Date class.
17.
Definition
Creation
timeline
Programs
Quiz
Time
follow
Program on SystemDefined packages
// Importing classes from system-defined packages
import java.util.ArrayList;
import java.util.Collections;
class SystemPackageExample {
public static void main(String[] args) {
// Creating an ArrayList from java.util package
ArrayList<String> names = new ArrayList<>();
// Adding some names to the list
names.add("Alice");
names.add("Bob");
names.add("Charlie");
names.add("Aman");
names.add("Jhon");
// Sorting the list using Collections from java.util package
Collections.sort(names);
// Printing the sorted list
System.out.println("Sorted Names:");
for (String name : names) {
System.out.println(name);
}
}
}
Program on Userdefined Packages:
package myfirstpackage;
public class Operations {
public static int add(int a, int b) {
return a + b;
}
public static int subtract(int a, int b) {
return a - b;
}
public static int multiply(int a, int b) {
return a * b; }
public static int divide(int a, int b) {
if (b == 0) {
throw new
IllegalArgumentException("Division by zero is
not allowed.");
}
return a / b;
}
}
20.
Model 1: importpackage.*;
import myfirstpackage.*;
import java.util.*;
public class WildcardImportExample {
public static void main(String[] args) {
//int a = 10;
//int b = 5;
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Number : ");
System.out.println("Enter Second Number : ");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("Addition: " + Operations.add(a, b));
System.out.println("Subtraction: " + Operations.subtract(a, b));
System.out.println("Multiplication: " + Operations.multiply(a, b));
System.out.println("Division: " + Operations.divide(a, b));
}
}
Model 2: importpackage.classname;
import myfirstpackage.Operations;
public class SingleTypeImportExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println("Addition: " + Operations.add(a, b));
System.out.println("Subtraction: " + Operations.subtract(a, b));
System.out.println("Multiplication: " + Operations.multiply(a, b));
System.out.println("Division: " + Operations.divide(a, b));
}
}
23.
Model 3: Usingfully qualified name
public class TestOperations {
public static void main(String[] args) {
// Using fully qualified name
int sum = myfirstpackage.Operations.add(5, 3);
int difference = myfirstpackage.Operations.subtract(5, 3);
int product = myfirstpackage.Operations.multiply(5, 3);
int quotient = myfirstpackage.Operations.divide(6, 3);
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
}
}