SlideShare a Scribd company logo
Prepared by
Monika Mishra
Packages In Java
 Packages in Java are a way of grouping similar types of classes
together.
 A package basically acts as a container for group of related classes.
 Package = directory. A package name is the same as the directory
(folder) name which contains the .java files.
 It is a great way to achieve reusability.
 We can simply import a class providing the required functionality from
an existing package and use it in our program.
Package
awt
java Package containing
awt package
Awt Package containing
classes
classes in awt package
Color
Graphics
Rectangle
Panel
Label
Color
Grid layout
Java
awt
 The concept of package can be considered as means to achieve data
encapsulation.
 A package may consists of a lot of classes but only few needs to be
exposed as most of them are required internally.
 To access package import statement is required and
package names is separated by dot, e.g.,import java.lang.*;
 Thus, we can hide the classes and prevent programs or other
packages from accessing classes which are meant for internal usage
only.
 Packages Avoid name space collision. There can not be two classes
with same name in a same Package But two packages can have a
class with same name.
 The JOptionPane class is in the swing package, which is located in
the javax package. The wildcard character (*) is used to specify
that all classes with that package are available to your program.
This is the most common programming style.
METHOD 1
import javax.swing.*; // Make all classes visible altho only one is
used.
class ImportTest
{
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Hi");
System.exit(0);
}
}
Imports: three options
METHOD 2
Classes can be specified explicitly on import instead of using the
wildcard character.
import javax.swing.JOptionPane; // Make a single class visible.
class ImportTest {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Hi");
System.exit(0);
}
}
METHOD 3
Alternately we can the fully qualified class name without an import.
class ImportTest {
public static void main(String[] args) {
javax.swing.JOptionPane.showMessageDialog(null, "Hi");
System.exit(0);
}
}
 Packages are categorized as :

1 ) Pre-defined packages ( standard packages which come as a
part of Java Runtime Environment )
2 ) User-defined packages ( packages defined by programmers
to bundle group of related classes )
lang util rmi
io sql text
awt beans security
applet math time
net nio
Pre defined Packages
 It is a collection of predefined classes and interfaces.
 Java contain total 14 predefined packages.
14 predefined packages
150 sub packages
min 7 thousand classes
min 7 lakh methods
.
14 Pre Defined Packages in Java
1) java.lang
2) java.io
5) java.net
8) java.beans
9) java.math
10) java.nio
11)java. rmi
12) java.text
13) java.security
14) java.time
3)java. awt
4)java. applet
6) java.util
7) java. sql
14 Pre defined packages
Packages Description
Java.lang It is a default package which contain primitive data type, displaying
result on console screen, obtaining garbage collector etc.
java.io It used for developing file handling applications, such as, opening the
file in read or write mode, reading or writing the data, etc.
java.awt This package is used for developing GUI (Graphic User Interface)
components such as buttons, check boxes, scroll boxes, etc.
Java. applet This package is used for developing browser oriented applications.
java.net This package is used for developing client server applications.
java.util Contains utility classes which implement data structures like Hash
Table, Dictionary, etc.
java.sql This package is used for retrieving the data from data base and
performing various operations on data base.
Packages Description
java.beans It contains classes and interfaces related to JavaBeans
components.(java components contain following feature: A
no-argument constructor, Class must not define any public
instance variables)
java.math It provides classes for performing arbitrary-precision integer
arithmetic (BigInteger) and arbitrary-precision decimal
arithmetic (BigDecimal).
java.nio Defines buffers, which are containers for data, and provides
an overview of the other NIO packages.
java.rmi RMI is Remote Method Invocation. It is a mechanism that
enables an object on one Java virtual machine to invoke
methods on an object in another Java virtual machine.
java.text This package is used for formatting date and time on day to
day business operations.
java.security
Provides the classes and interfaces for the security framework.
This includes classes that implement an easily configurable,
fine-grained access control security architecture.
Java. time This package is used for dates, times, instants, and durations.
(included in java 1.8 Version)
How to check Pre defined methods
in pre defined class
Syntax:
javap <fully qualified name>
Example:
If we want to see all the methods present in String
class.
javap java.lang.String
output
User-defined packages
The users of the Java language can also create their
own packages. They are called user-defined packages.
User defined packages can also be imported into
other classes & used exactly in the same way as the
Built in packages.
Syntax of package
package <package name>;
Example :
package college;
 We must first declare the name of the package using the package
keyword followed by the package name.
 while declaring the package every character must be in lower case.
 The source file allow to declare only one package and it must be the
first statement in a Java source file.
 Then define a classes as normally as define a class.
Methods to write/create a package
When package and Main
function both are in same
program
When package and Main
function both are in different
program
Java file1
package packageName;
public class className
{
- - - - - - - - - - - - -
// Body of className
- - - - - - - - - - - -
main
{
- - - - - - - - - - - - -
// Body of main
- - - - - - - - - - - -
}
}
Java file1
package packageName;
public class className1
{
- - - - - - - - - - - - -
// Body of className
- - - - - - - - - - - -
}
Java file2
class className2
{
main
{
- - - - - - - - - - - - -
// Body of main
- - - - - - - - - - - -
}
When package and Main
function both are in same
program
When package and Main
function both are in different
program
 In main function we can
create and use only single
package.
 We can’t add a new class in
existing package.
 Main is present.
NOTE:
We cannot import and create
object of package program,
Since main function is already
created in package program.
 Multiple packages can be
created ,in main function we
can use all these packages
using import statement.
 We can add multiple classes
in existing package.
 Main is absent.
Creating User Defined Packages
When package and Main both are in same program
SYNTAX 1:
package packageName;
public class className
{
- - - - - - - - - - - - -
// Body of className
- - - - - - - - - - - -
main
{
- - - - - - - - - - - - -
// Body of main
- - - - - - - - - - - -
}
}
NOTE:
Here use of “public” is optional because
package and main class both are in same
program.
Steps to create package
 Step 1: create a Directory/Folder where you store all your
package programs.
EX: g:packageprogram1
 Step 2: create a java file.
EX: Test.java
 Step 3: Write the following code in Test file
package p1;
public class Test
{
public static void main(String args[])
{
System.out.println("package first example");
}
}
NOTE:
Here use of “public” is optional because
package and main class both are in same
program.
 Step4: There are two ways to compile the file
 javac –d . Test.java
javac => java compiler
-d => create folder
. => place the folder in
current working
directory
Test.java => File name
javac –d g:packageprogram1 Test.java
javac => java compiler
-d => create folder
g: packageprogram1 => place the folder in g:packageprogram1 directory
Test.java => File name
Using classpath
setclasspath=G:packageprogram2
javac -d . Test.java
javac => java compiler
-d => create folder
. => place the folder in
current working
directory
Test.java => File name
 Execution Process
1. java <fully qualified name>
java p1.Test
Creating multiple classes when package and Main both are in same
program
SYNTAX 2:
package myPackage;
class class1
{
- - - - - - - - - - - - -
// Body of class1
}
class class2
{
- - - - - - - - - -- - -
// Body of class2
}
class class3
{
- - - - - - - - - -- - -
// Body of class3
}
NOTE:
•We can create multiple classes in same
package.
•Here use of “public” is optional because
package and main class both are in same
program.
main
{
- - - - - - - - - - - - -
// Body of main
- - - - - - - - - - - -
}
Steps to create package
 Step 1: create a Directory/Folder where you store all your
package programs.
EX: g:packageprogram2
 Step 2: create a java file.
EX: Test.java
 Step 3: Write the following code in Test file
package p2;
class Test
{
public static void main(String args[])
{
System.out.println("package second example");
}
}
class A
{
}
class B
{
}
class c
{
}
NOTE:
•We can create multiple classes in same
package.
•Here use of “public” is optional because
package and main class both are in same
program.
 Step4: There are two ways to compile the file when Programmer is
1) In current directory
2) Not in current directory
In current directory
i. javac –d . Test.java
javac => java compiler
-d => create folder
. => place the folder in
current working
directory
Test.java => File name
ii. javac –d g:packageprogram2 Test.java
javac => java compiler
-d => create folder
g: packageprogram2 => place the folder in g:packageprogram2 directory
Test.java => File name
Not in current directory
Using classpath
set classpath=G:packageprogram2
javac -d . Test.java
javac => java compiler
-d => create folder
. => place the folder in
current working
directory
Test.java => File name
 Execution Process
1. java <fully qualified name>
java p2.Test
CLASSPATH:
Classpath is a parameter in the Java Virtual Machine or
the Java Compiler that specifies the location of user-
defined classes and packages.
To set CLASSPATH in Java you need to include all those
directories where you have put either your .class file
or JAR file which is required by your Java application.
To check the current setting of the CLASSPATH, issue the following
command:
>set classpath
CLASSPATH can be set temporarily for that particular CMD shell
session by issuing the following command:
>SET CLASSPATH=c:javaprojectclasses
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:javaprojectclasses
com.abc.project1.subproject2.MyClass3
Difference between path and
classpath
 Path variable is set for providing path for all Java tools like java,
javac, javap, javah, jar, appletviewer. In Java to run any program
we use java tool and for compile Java code use javac tool. All
these tools are available in bin folder so we set path upto bin
folder.
classpath variable is set for providing path of all Java
classes which is used in our application. All classes are available in
lib/rt.jar so we set classpath upto lib/rt.jar.
 The main difference between PATH and CLASSPATH is
that PATH is an environment variable which is used to locate
JDK binaries like "java" or "javac" command used to run java
program and compile java source file. On the other hand,
CLASSPATH, an environment variable is used by System or
Application ClassLoader to locate and load compile Java
bytecodes stored in the .class file.
 In order to set PATH in Java, you need to include
JDK_HOME/bin directory in PATH environment variable while
in order to set CLASSPATH in Java you need to include all those
directories where you have put either your .class file or JAR file
which is required by your Java application.
 Another significant difference between PATH and
CLASSPATH is that PATH can not be overridden by any Java
settings but CLASSPATH can be overridden by providing
command line option -classpath or -cp to both "java" and
"javac" commands or by using Class-Path attribute in Manifest
file inside JAR archive.
 PATH environment variable is used by operating system to
find any binary or command typed in the shell, this is true for
both Windows and Linux environment while CLASSPATH is
only used by Java ClassLoaders to load class files.
When Main and package are in different
files
SYNTAX 1:
Java File1
package packageName;
public class className
{
- - - - - - - - - - -
// Body of className
- - - - - - - - - - - -
}
NOTE:
Here use of “public “ keyword is
compulsory ,so that it can be
accessed from any other class.
Java File2
import packageName.className;
main
{
- - - - - - - - - - - - -
// Body of main
- - - - - - - - - - - -
}
NOTE:
In place of className you can also use
wildcard character (*) to include all
classes of that package.
Steps to create package
 Step 1: create a Directory/Folder where you store all your
package programs.
EX: E:packageprogram3
 Step 2: create first java file.
EX: AA.java
 Step 3: Write the following code in AA.java file.
package p3;
public class AA{
public void msg1()
{
System.out.println(“My third package");
}
}
 Step 4: Save the file.
 Step 5: compile AA.java
file.
NOTE:
Here use of “public “ keyword is
compulsory ,so that it can be
accessed from any other class.
). javac –d e:packageprogram3 AA.java
javac => java compiler
-d => create folder
E: packageprogram3 => place the folder in g:packageprogram3 directory
AA.java => File name
Steps to add new class into existing
package
Step1: create a java file
CC.java
Step2: Write the following code in java File
package p3;
public class CC{
public void msg2()
{
System.out.println(“user define package");
}
}
 Step 4: Save the file.
 Step 5: compile CC.java
file.
 Step 6: create Third java
file.
EX: BB.java
 Step 7:Write the following code in BB.java file.
import p3.*; //import p3.AA; import p3.CC;
class BB {
public static void main(String args[]){
AA obj1 = new AA();
obj1.msg1();
CC obj2 = new CC();
obj2.msg2();
}
}
 Step 8: Save the file.
 Step 9: compile BB.java file using class path
set classpath=e:packageprogram3
javac BB.java
 Step 10: Run BB.java file using class path
set classpath=e:packageprogram3
java BB
But now, the JRE can't even find the BB class, which is located in
the current directory. This is because if CLASSPATH is not
explicitly set, it defaulted to the current directory.
However, if CLASSPATH is explicitly set, it does not include the
current directory unless the current directory is included. Hence,
we need to include current directory (denoted as '.') in the
CLASSPATH, together with the base directory of package p3,
separated by ';', as follows:
 Step 10: Run BB.java file using class path
set classpath=e:packageprogram3;.
java BB
Steps to create package in current working
directory
 Step 1: create a Directory/Folder where you store all your
package programs.
EX: E:packageprogram3
 Step 2: create first java file.
EX: AA.java
 Step 3: Write the following code in AA.java file.
package p3;
public class AA{
public void msg1()
{
System.out.println(“My third package");
}
}
 Step 4: Save the file.
 Step 5: compile AA.java
file.
NOTE:
Here use of “public “ keyword is
compulsory ,so that it can be
accessed from any other class.
). javac –d . AA.java
javac => java compiler
-d => create folder
.=> place the folder in current working directory
AA.java => File name
 Step 6: create Second
java file.
EX: BB.java
 Step 7:Write the following code in BB.java file.
import p3.AA;
class BB {
public static void main(String args[]){
AA obj1 = new AA();
obj1.msg1();
}
}
 Step 8: Save the file.
 Step 9: compile BB.java file
javac BB.java
 Step 10: Run BB.java file using class path
java BB
Creating Package inside another Package
 Step 1: create a Directory/Folder where you store all your
package programs.
EX: E:packageprogram4
 Step 2: create first java file.
EX: AA.java
 Step 3: Write the following code in AA.java file.
package pack1.pack2;
public class AA
{
public void display()
{
System.out.println("creating package inside another package");
}
}
 Step 4: Save the file.
 Step 5: compile AA.java
file.
). javac –d e:packageprogram4 AA.java
javac => java compiler
-d => create folder
E: packageprogram4 => place the folder in g:packageprogram4 directory
AA.java => File name
Steps to add new class into existing
package
Step1: create a java file
CC.java
Step2: Write the following code in java File
package pack1.pack2;
public class CC
{
public void msg2()
{
System.out.println("user define package");
}
}
 Step 4: Save the file.
 Step 5: compile CC.java
file.
 Step 6: create Third java
file.
EX: BB.java
 Step 7:Write the following code in BB.java
file.
import pack1.pack2.*;
class BB {
public static void main(String args[]){
AA obj1= new AA();
obj1.msg1();
CC obj2= new CC();
obj2.msg2();
}
}
 Step 8: Save the file.
 Step 9: compile BB.java file using class path
set classpath=e:packageprogram4
javac BB.java
 Step 10: Run BB.java file using class path
set classpath=e:packageprogram3;.
java BB
Steps to create package in current working
directory
 Step 1: create a Directory/Folder where you store all your
package programs.
EX: E:packageprogram4
 Step 2: create first java file.
EX: AA.java
 Step 3: Write the following code in AA.java file.
package pack1.pack2;
public class AA
{
public void display()
{
System.out.println("creating package inside another package");
}
}
 Step 4: Save the file.
 Step 5: compile AA.java
file.
NOTE:
Here use of “public “ keyword is
compulsory ,so that it can be
accessed from any other class.
). javac –d . AA.java
javac => java compiler
-d => create folder
.=> place the folder in current working directory
AA.java => File name
 Step 6: create Second
java file.
EX: BB.java
 Step 7:Write the following code in BB.java file.
import pack1.pack2.AA;
class BB {
public static void main(String args[]){
AA obj = new AA();
obj.display();
}
}
 Step 8: Save the file.
 Step 9: compile BB.java file
javac BB.java
 Step 10: Run BB.java file using class path
java BB
THANK
YOU…!

More Related Content

What's hot (20)

Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
File handling
File handlingFile handling
File handling
 
Files in java
Files in javaFiles in java
Files in java
 
Java package
Java packageJava package
Java package
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Java IO
Java IOJava IO
Java IO
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Java virtual machine
Java virtual machineJava virtual machine
Java virtual machine
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
 
Java tokens
Java tokensJava tokens
Java tokens
 
Packages in java
Packages in javaPackages in java
Packages in java
 
QSpiders - Jdk Jvm Jre and Jit
QSpiders - Jdk Jvm Jre and JitQSpiders - Jdk Jvm Jre and Jit
QSpiders - Jdk Jvm Jre and Jit
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Scanner class
Scanner classScanner class
Scanner class
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
Java I/O
Java I/OJava I/O
Java I/O
 

Similar to Packages

Similar to Packages (20)

7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
20-packages-jar.ppt
20-packages-jar.ppt20-packages-jar.ppt
20-packages-jar.ppt
 
Lecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptLecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Packages
PackagesPackages
Packages
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 

Recently uploaded

shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxVishalDeshpande27
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwoodseandesed
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdfKamal Acharya
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdfKamal Acharya
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringC Sai Kiran
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerapareshmondalnita
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionjeevanprasad8
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edgePaco Orozco
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfAbrahamGadissa
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdfKamal Acharya
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Krakówbim.edu.pl
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptssuser9bd3ba
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageRCC Institute of Information Technology
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxwendy cai
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfKamal Acharya
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdfAhmedHussein950959
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-IVigneshvaranMech
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.PrashantGoswami42
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsAtif Razi
 

Recently uploaded (20)

shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 

Packages

  • 2. Packages In Java  Packages in Java are a way of grouping similar types of classes together.  A package basically acts as a container for group of related classes.  Package = directory. A package name is the same as the directory (folder) name which contains the .java files.  It is a great way to achieve reusability.  We can simply import a class providing the required functionality from an existing package and use it in our program.
  • 3. Package awt java Package containing awt package Awt Package containing classes classes in awt package Color Graphics Rectangle Panel Label Color Grid layout Java awt
  • 4.  The concept of package can be considered as means to achieve data encapsulation.  A package may consists of a lot of classes but only few needs to be exposed as most of them are required internally.  To access package import statement is required and package names is separated by dot, e.g.,import java.lang.*;  Thus, we can hide the classes and prevent programs or other packages from accessing classes which are meant for internal usage only.  Packages Avoid name space collision. There can not be two classes with same name in a same Package But two packages can have a class with same name.
  • 5.  The JOptionPane class is in the swing package, which is located in the javax package. The wildcard character (*) is used to specify that all classes with that package are available to your program. This is the most common programming style. METHOD 1 import javax.swing.*; // Make all classes visible altho only one is used. class ImportTest { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hi"); System.exit(0); } } Imports: three options
  • 6. METHOD 2 Classes can be specified explicitly on import instead of using the wildcard character. import javax.swing.JOptionPane; // Make a single class visible. class ImportTest { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hi"); System.exit(0); } } METHOD 3 Alternately we can the fully qualified class name without an import. class ImportTest { public static void main(String[] args) { javax.swing.JOptionPane.showMessageDialog(null, "Hi"); System.exit(0); } }
  • 7.  Packages are categorized as :  1 ) Pre-defined packages ( standard packages which come as a part of Java Runtime Environment ) 2 ) User-defined packages ( packages defined by programmers to bundle group of related classes ) lang util rmi io sql text awt beans security applet math time net nio
  • 8. Pre defined Packages  It is a collection of predefined classes and interfaces.  Java contain total 14 predefined packages. 14 predefined packages 150 sub packages min 7 thousand classes min 7 lakh methods
  • 9. . 14 Pre Defined Packages in Java 1) java.lang 2) java.io 5) java.net 8) java.beans 9) java.math 10) java.nio 11)java. rmi 12) java.text 13) java.security 14) java.time 3)java. awt 4)java. applet 6) java.util 7) java. sql
  • 10. 14 Pre defined packages Packages Description Java.lang It is a default package which contain primitive data type, displaying result on console screen, obtaining garbage collector etc. java.io It used for developing file handling applications, such as, opening the file in read or write mode, reading or writing the data, etc. java.awt This package is used for developing GUI (Graphic User Interface) components such as buttons, check boxes, scroll boxes, etc. Java. applet This package is used for developing browser oriented applications. java.net This package is used for developing client server applications. java.util Contains utility classes which implement data structures like Hash Table, Dictionary, etc. java.sql This package is used for retrieving the data from data base and performing various operations on data base.
  • 11. Packages Description java.beans It contains classes and interfaces related to JavaBeans components.(java components contain following feature: A no-argument constructor, Class must not define any public instance variables) java.math It provides classes for performing arbitrary-precision integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic (BigDecimal). java.nio Defines buffers, which are containers for data, and provides an overview of the other NIO packages. java.rmi RMI is Remote Method Invocation. It is a mechanism that enables an object on one Java virtual machine to invoke methods on an object in another Java virtual machine. java.text This package is used for formatting date and time on day to day business operations. java.security Provides the classes and interfaces for the security framework. This includes classes that implement an easily configurable, fine-grained access control security architecture. Java. time This package is used for dates, times, instants, and durations. (included in java 1.8 Version)
  • 12. How to check Pre defined methods in pre defined class Syntax: javap <fully qualified name> Example: If we want to see all the methods present in String class. javap java.lang.String
  • 14. User-defined packages The users of the Java language can also create their own packages. They are called user-defined packages. User defined packages can also be imported into other classes & used exactly in the same way as the Built in packages.
  • 15. Syntax of package package <package name>; Example : package college;  We must first declare the name of the package using the package keyword followed by the package name.  while declaring the package every character must be in lower case.  The source file allow to declare only one package and it must be the first statement in a Java source file.  Then define a classes as normally as define a class.
  • 16. Methods to write/create a package When package and Main function both are in same program When package and Main function both are in different program Java file1 package packageName; public class className { - - - - - - - - - - - - - // Body of className - - - - - - - - - - - - main { - - - - - - - - - - - - - // Body of main - - - - - - - - - - - - } } Java file1 package packageName; public class className1 { - - - - - - - - - - - - - // Body of className - - - - - - - - - - - - } Java file2 class className2 { main { - - - - - - - - - - - - - // Body of main - - - - - - - - - - - - }
  • 17. When package and Main function both are in same program When package and Main function both are in different program  In main function we can create and use only single package.  We can’t add a new class in existing package.  Main is present. NOTE: We cannot import and create object of package program, Since main function is already created in package program.  Multiple packages can be created ,in main function we can use all these packages using import statement.  We can add multiple classes in existing package.  Main is absent.
  • 18. Creating User Defined Packages When package and Main both are in same program SYNTAX 1: package packageName; public class className { - - - - - - - - - - - - - // Body of className - - - - - - - - - - - - main { - - - - - - - - - - - - - // Body of main - - - - - - - - - - - - } } NOTE: Here use of “public” is optional because package and main class both are in same program.
  • 19. Steps to create package  Step 1: create a Directory/Folder where you store all your package programs. EX: g:packageprogram1  Step 2: create a java file. EX: Test.java
  • 20.  Step 3: Write the following code in Test file package p1; public class Test { public static void main(String args[]) { System.out.println("package first example"); } } NOTE: Here use of “public” is optional because package and main class both are in same program.
  • 21.  Step4: There are two ways to compile the file  javac –d . Test.java javac => java compiler -d => create folder . => place the folder in current working directory Test.java => File name javac –d g:packageprogram1 Test.java javac => java compiler -d => create folder g: packageprogram1 => place the folder in g:packageprogram1 directory Test.java => File name
  • 22. Using classpath setclasspath=G:packageprogram2 javac -d . Test.java javac => java compiler -d => create folder . => place the folder in current working directory Test.java => File name
  • 23.  Execution Process 1. java <fully qualified name> java p1.Test
  • 24. Creating multiple classes when package and Main both are in same program SYNTAX 2: package myPackage; class class1 { - - - - - - - - - - - - - // Body of class1 } class class2 { - - - - - - - - - -- - - // Body of class2 } class class3 { - - - - - - - - - -- - - // Body of class3 } NOTE: •We can create multiple classes in same package. •Here use of “public” is optional because package and main class both are in same program. main { - - - - - - - - - - - - - // Body of main - - - - - - - - - - - - }
  • 25. Steps to create package  Step 1: create a Directory/Folder where you store all your package programs. EX: g:packageprogram2  Step 2: create a java file. EX: Test.java
  • 26.  Step 3: Write the following code in Test file package p2; class Test { public static void main(String args[]) { System.out.println("package second example"); } } class A { } class B { } class c { } NOTE: •We can create multiple classes in same package. •Here use of “public” is optional because package and main class both are in same program.
  • 27.  Step4: There are two ways to compile the file when Programmer is 1) In current directory 2) Not in current directory In current directory i. javac –d . Test.java javac => java compiler -d => create folder . => place the folder in current working directory Test.java => File name ii. javac –d g:packageprogram2 Test.java javac => java compiler -d => create folder g: packageprogram2 => place the folder in g:packageprogram2 directory Test.java => File name
  • 28. Not in current directory Using classpath set classpath=G:packageprogram2 javac -d . Test.java javac => java compiler -d => create folder . => place the folder in current working directory Test.java => File name
  • 29.  Execution Process 1. java <fully qualified name> java p2.Test
  • 30.
  • 31. CLASSPATH: Classpath is a parameter in the Java Virtual Machine or the Java Compiler that specifies the location of user- defined classes and packages. To set CLASSPATH in Java you need to include all those directories where you have put either your .class file or JAR file which is required by your Java application.
  • 32. To check the current setting of the CLASSPATH, issue the following command: >set classpath CLASSPATH can be set temporarily for that particular CMD shell session by issuing the following command: >SET CLASSPATH=c:javaprojectclasses 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:javaprojectclasses com.abc.project1.subproject2.MyClass3
  • 33. Difference between path and classpath  Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer. In Java to run any program we use java tool and for compile Java code use javac tool. All these tools are available in bin folder so we set path upto bin folder. classpath variable is set for providing path of all Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.
  • 34.
  • 35.  The main difference between PATH and CLASSPATH is that PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. On the other hand, CLASSPATH, an environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in the .class file.  In order to set PATH in Java, you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directories where you have put either your .class file or JAR file which is required by your Java application.
  • 36.  Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands or by using Class-Path attribute in Manifest file inside JAR archive.  PATH environment variable is used by operating system to find any binary or command typed in the shell, this is true for both Windows and Linux environment while CLASSPATH is only used by Java ClassLoaders to load class files.
  • 37. When Main and package are in different files SYNTAX 1: Java File1 package packageName; public class className { - - - - - - - - - - - // Body of className - - - - - - - - - - - - } NOTE: Here use of “public “ keyword is compulsory ,so that it can be accessed from any other class. Java File2 import packageName.className; main { - - - - - - - - - - - - - // Body of main - - - - - - - - - - - - } NOTE: In place of className you can also use wildcard character (*) to include all classes of that package.
  • 38. Steps to create package  Step 1: create a Directory/Folder where you store all your package programs. EX: E:packageprogram3  Step 2: create first java file. EX: AA.java  Step 3: Write the following code in AA.java file.
  • 39. package p3; public class AA{ public void msg1() { System.out.println(“My third package"); } }  Step 4: Save the file.  Step 5: compile AA.java file. NOTE: Here use of “public “ keyword is compulsory ,so that it can be accessed from any other class. ). javac –d e:packageprogram3 AA.java javac => java compiler -d => create folder E: packageprogram3 => place the folder in g:packageprogram3 directory AA.java => File name
  • 40.
  • 41. Steps to add new class into existing package Step1: create a java file CC.java Step2: Write the following code in java File package p3; public class CC{ public void msg2() { System.out.println(“user define package"); } }
  • 42.  Step 4: Save the file.  Step 5: compile CC.java file.  Step 6: create Third java file. EX: BB.java
  • 43.  Step 7:Write the following code in BB.java file. import p3.*; //import p3.AA; import p3.CC; class BB { public static void main(String args[]){ AA obj1 = new AA(); obj1.msg1(); CC obj2 = new CC(); obj2.msg2(); } }  Step 8: Save the file.
  • 44.  Step 9: compile BB.java file using class path set classpath=e:packageprogram3 javac BB.java  Step 10: Run BB.java file using class path set classpath=e:packageprogram3 java BB
  • 45. But now, the JRE can't even find the BB class, which is located in the current directory. This is because if CLASSPATH is not explicitly set, it defaulted to the current directory. However, if CLASSPATH is explicitly set, it does not include the current directory unless the current directory is included. Hence, we need to include current directory (denoted as '.') in the CLASSPATH, together with the base directory of package p3, separated by ';', as follows:  Step 10: Run BB.java file using class path set classpath=e:packageprogram3;. java BB
  • 46. Steps to create package in current working directory  Step 1: create a Directory/Folder where you store all your package programs. EX: E:packageprogram3  Step 2: create first java file. EX: AA.java  Step 3: Write the following code in AA.java file.
  • 47. package p3; public class AA{ public void msg1() { System.out.println(“My third package"); } }  Step 4: Save the file.  Step 5: compile AA.java file. NOTE: Here use of “public “ keyword is compulsory ,so that it can be accessed from any other class. ). javac –d . AA.java javac => java compiler -d => create folder .=> place the folder in current working directory AA.java => File name
  • 48.  Step 6: create Second java file. EX: BB.java  Step 7:Write the following code in BB.java file. import p3.AA; class BB { public static void main(String args[]){ AA obj1 = new AA(); obj1.msg1(); } }
  • 49.  Step 8: Save the file.  Step 9: compile BB.java file javac BB.java  Step 10: Run BB.java file using class path java BB
  • 50. Creating Package inside another Package  Step 1: create a Directory/Folder where you store all your package programs. EX: E:packageprogram4  Step 2: create first java file. EX: AA.java  Step 3: Write the following code in AA.java file.
  • 51. package pack1.pack2; public class AA { public void display() { System.out.println("creating package inside another package"); } }  Step 4: Save the file.  Step 5: compile AA.java file. ). javac –d e:packageprogram4 AA.java javac => java compiler -d => create folder E: packageprogram4 => place the folder in g:packageprogram4 directory AA.java => File name
  • 52.
  • 53. Steps to add new class into existing package Step1: create a java file CC.java Step2: Write the following code in java File package pack1.pack2; public class CC { public void msg2() { System.out.println("user define package"); } }
  • 54.  Step 4: Save the file.  Step 5: compile CC.java file.  Step 6: create Third java file. EX: BB.java
  • 55.  Step 7:Write the following code in BB.java file. import pack1.pack2.*; class BB { public static void main(String args[]){ AA obj1= new AA(); obj1.msg1(); CC obj2= new CC(); obj2.msg2(); } }  Step 8: Save the file.
  • 56.  Step 9: compile BB.java file using class path set classpath=e:packageprogram4 javac BB.java  Step 10: Run BB.java file using class path set classpath=e:packageprogram3;. java BB
  • 57. Steps to create package in current working directory  Step 1: create a Directory/Folder where you store all your package programs. EX: E:packageprogram4  Step 2: create first java file. EX: AA.java  Step 3: Write the following code in AA.java file.
  • 58. package pack1.pack2; public class AA { public void display() { System.out.println("creating package inside another package"); } }  Step 4: Save the file.  Step 5: compile AA.java file. NOTE: Here use of “public “ keyword is compulsory ,so that it can be accessed from any other class. ). javac –d . AA.java javac => java compiler -d => create folder .=> place the folder in current working directory AA.java => File name
  • 59.  Step 6: create Second java file. EX: BB.java  Step 7:Write the following code in BB.java file. import pack1.pack2.AA; class BB { public static void main(String args[]){ AA obj = new AA(); obj.display(); } }
  • 60.  Step 8: Save the file.  Step 9: compile BB.java file javac BB.java  Step 10: Run BB.java file using class path java BB