SlideShare a Scribd company logo
PACKAGES IN JAVA
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined
package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql
etc.
Advantage of Java Package:
 Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
 Java package provides access protection.
Java package removes naming collision.
Simple example of java package:
The package keyword is used to create a package in java.
//save as Simple.java
package mypack;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
• How to access package from another package?
• There are three ways to access the package from outside
the package.
1.import package.*;
2.import package.classname;
3.fully qualified name.
• 1) Using packagename.*
• If you use package.* then all the classes and interfaces
of this package will be accessible but not subpackages.
• The import keyword is used to make the classes and
interface of another package accessible to the current
package.
Example of package that import the packagename.*
//save by A.java
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
//save by B.java
package mypack;
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
2) Using packagename.classname:
If you import package.classname then only declared class of this
package will be accessible.
Example of package by import package.classname
//save by A.java
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
//save by B.java
package mypack;
import pack.A;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
• 3) Using fully qualified name
• If you use fully qualified name then only declared class of this package will be
accessible. Now there is no need to import. But you need to use fully qualified
name every time when you are accessing the class or interface.
• It is generally used when two packages have same class name e.g. java.util
and java.sql packages contain Date class.
• Example of package by import fully qualified name
1.//save by A.java
2.package pack;
3.public class A{
4. public void msg(){System.out.println("Hello");}
5.}
6.//save by B.java
7.package mypack;
8.class B{
9. public static void main(String args[]){
10. pack.A obj = new pack.A();//using fully qualified name
11. obj.msg();
12. }
13.}
• Note: Sequence of the program must be package then import
then class.
Subpackage in java:
Package inside the package is called the subpackage. It should be
created to categorize the package further.
Sun Microsystem has definded a package named java that contains
many classes like System, String, Reader, Writer, Socket etc.
These classes represent a particular group e.g. Reader and Writer
classes are for Input/Output operation, Socket and ServerSocket
classes are for networking etc and so on.
So, Sun Microsoft has subcategorized the java package into
subpackages such as lang, net, io etc.
The Input/Output related classes in io package.
Server and ServerSocket classes in net packages and so on.
Example of Subpackage:
package com.javatpoint.core;
class Simple
{
public static void main(String args[])
{
System.out.println("Hello subpackage");
}
}
To Compile: javac -d . Simple.java
To Run: java com.javatpoint.core.Simple
Output:Hello subpackage
How to send the class file to another directory or drive?
The class file of A.java source file in classes folder of c: drive.
For example:
//save as Simple.java
package mypack;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
To run this program from e:source directory, you need to set
classpath of the directory where the class file resides.
e:sources> set classpath=c:classes;.;
e:sources> java mypack.Simple
To Compile:
e:sources> javac -d c:classes Simple.java
To Run:
• Another way to run this program by -classpath switch of java:
• The -classpath switch can be used with javac and java tool.
• To run this program from e:source directory, you can use -
classpath switch of java that tells where to look for class file.
For example:
• e:sources> java -classpath c:classes mypack.Simple
Output:Welcome to package
There are two ways to load the class
files temporary and permanent.
Ways to load the class files or jar files
•Temporary
•By setting the classpath in the command prompt
•By -classpath switch
•Permanent
•By setting the classpath in the environment variables
•By creating the jar file, that contains all the class
files, and copying the jar file in the jre/lib/ext folder.

More Related Content

What's hot

Oops in Java
Oops in JavaOops in Java
Oops in Java
malathip12
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Data types in java
Data types in javaData types in java
Data types in java
HarshitaAshwani
 
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
Java packages
Java packagesJava packages
Java packages
Raja Sekhar
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
ParvizMirzayev2
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
Methods in java
Methods in javaMethods in java
Methods in java
chauhankapil
 
Files in java
Files in javaFiles in java
Java program structure
Java program structureJava program structure
Java program structure
shalinikarunakaran1
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Darpan Chelani
 

What's hot (20)

Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Data types in java
Data types in javaData types in java
Data types in java
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
 
Java packages
Java packagesJava packages
Java packages
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Files in java
Files in javaFiles in java
Files in java
 
Java program structure
Java program structureJava program structure
Java program structure
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 

Similar to Packages in 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
Kavitha713564
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersOOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
RatnaJava
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
happycocoman
 
Packages
PackagesPackages
Packages
Monika Mishra
 
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
lirogal422
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
Hitesh-Java
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersSession 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access Modifiers
PawanMM
 
Packages
PackagesPackages
Packages
Nuha Noor
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
Kawsar Hamid Sumon
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
SanthiNivas
 
Packages
PackagesPackages
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 
Packages
PackagesPackages
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
Victer Paul
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
Packages in java
Packages in javaPackages in java
Packages in java
SahithiReddyEtikala
 
Java packags
Java packagsJava packags
Java packags
Padma Kannan
 
Package in Java
Package in JavaPackage in Java
Package in Java
lalithambiga kamaraj
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
VeenaNaik23
 

Similar to Packages in java (20)

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
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersOOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Packages
PackagesPackages
Packages
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
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
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersSession 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access Modifiers
 
Packages
PackagesPackages
Packages
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Packages
PackagesPackages
Packages
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Packages
PackagesPackages
Packages
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packags
Java packagsJava packags
Java packags
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 

More from Kavitha713564

The Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxThe Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptx
Kavitha713564
 
Programming in python in detail concept .pptx
Programming in python in detail concept .pptxProgramming in python in detail concept .pptx
Programming in python in detail concept .pptx
Kavitha713564
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
Kavitha713564
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptx
Kavitha713564
 
Interface in java
Interface in javaInterface in java
Interface in java
Kavitha713564
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Kavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
Kavitha713564
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
Kavitha713564
 
Basic of java
Basic of javaBasic of java
Basic of java
Kavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Arrays,string and vector
Arrays,string and vectorArrays,string and vector
Arrays,string and vector
Kavitha713564
 

More from Kavitha713564 (13)

The Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxThe Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptx
 
Programming in python in detail concept .pptx
Programming in python in detail concept .pptxProgramming in python in detail concept .pptx
Programming in python in detail concept .pptx
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptx
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
Basic of java
Basic of javaBasic of java
Basic of java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Arrays,string and vector
Arrays,string and vectorArrays,string and vector
Arrays,string and vector
 

Recently uploaded

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
AG2 Design
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
JezreelCabil2
 

Recently uploaded (20)

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
 

Packages in java

  • 1.
  • 2. PACKAGES IN JAVA A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Advantage of Java Package:  Java package is used to categorize the classes and interfaces so that they can be easily maintained.  Java package provides access protection. Java package removes naming collision.
  • 3.
  • 4. Simple example of java package: The package keyword is used to create a package in java. //save as Simple.java package mypack; public class Simple { public static void main(String args[]) { System.out.println("Welcome to package"); } }
  • 5. • How to access package from another package? • There are three ways to access the package from outside the package. 1.import package.*; 2.import package.classname; 3.fully qualified name. • 1) Using packagename.* • If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. • The import keyword is used to make the classes and interface of another package accessible to the current package.
  • 6. Example of package that import the packagename.* //save by A.java package pack; public class A { public void msg() { System.out.println("Hello"); } } //save by B.java package mypack; import pack.*; class B { public static void main(String args[]) { A obj = new A(); obj.msg(); } }
  • 7. 2) Using packagename.classname: If you import package.classname then only declared class of this package will be accessible. Example of package by import package.classname //save by A.java package pack; public class A { public void msg() { System.out.println("Hello"); } }
  • 8. //save by B.java package mypack; import pack.A; class B { public static void main(String args[]) { A obj = new A(); obj.msg(); } }
  • 9. • 3) Using fully qualified name • If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface. • It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class. • Example of package by import fully qualified name 1.//save by A.java 2.package pack; 3.public class A{ 4. public void msg(){System.out.println("Hello");} 5.} 6.//save by B.java 7.package mypack; 8.class B{ 9. public static void main(String args[]){ 10. pack.A obj = new pack.A();//using fully qualified name 11. obj.msg(); 12. } 13.}
  • 10. • Note: Sequence of the program must be package then import then class.
  • 11. Subpackage in java: Package inside the package is called the subpackage. It should be created to categorize the package further. Sun Microsystem has definded a package named java that contains many classes like System, String, Reader, Writer, Socket etc. These classes represent a particular group e.g. Reader and Writer classes are for Input/Output operation, Socket and ServerSocket classes are for networking etc and so on. So, Sun Microsoft has subcategorized the java package into subpackages such as lang, net, io etc. The Input/Output related classes in io package. Server and ServerSocket classes in net packages and so on.
  • 12. Example of Subpackage: package com.javatpoint.core; class Simple { public static void main(String args[]) { System.out.println("Hello subpackage"); } } To Compile: javac -d . Simple.java To Run: java com.javatpoint.core.Simple Output:Hello subpackage
  • 13. How to send the class file to another directory or drive? The class file of A.java source file in classes folder of c: drive. For example:
  • 14. //save as Simple.java package mypack; public class Simple { public static void main(String args[]) { System.out.println("Welcome to package"); } }
  • 15. To run this program from e:source directory, you need to set classpath of the directory where the class file resides. e:sources> set classpath=c:classes;.; e:sources> java mypack.Simple To Compile: e:sources> javac -d c:classes Simple.java To Run:
  • 16. • Another way to run this program by -classpath switch of java: • The -classpath switch can be used with javac and java tool. • To run this program from e:source directory, you can use - classpath switch of java that tells where to look for class file. For example: • e:sources> java -classpath c:classes mypack.Simple Output:Welcome to package
  • 17. There are two ways to load the class files temporary and permanent. Ways to load the class files or jar files •Temporary •By setting the classpath in the command prompt •By -classpath switch •Permanent •By setting the classpath in the environment variables •By creating the jar file, that contains all the class files, and copying the jar file in the jre/lib/ext folder.