SlideShare a Scribd company logo
1 of 61
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)

Java Strings
Java StringsJava Strings
Java Strings
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Java String
Java String Java String
Java String
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in javascript
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Java program structure
Java program structureJava program structure
Java program structure
 
File handling
File handlingFile handling
File handling
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Java packages
Java packagesJava packages
Java packages
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 

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 .
 
Java package
Java packageJava package
Java package
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Packages in java
Packages in javaPackages in java
Packages in 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
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
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
 
Packages in java
Packages in javaPackages in java
Packages in java
 
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
 

Recently uploaded

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 

Recently uploaded (20)

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 

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