Packages in Java allow grouping of related classes and avoid naming collisions. A package is mapped to a directory on the file system where Java files for that package are stored. There are predefined packages provided by Java and user-defined packages can also be created. Packages are specified using the package keyword and imported into other classes using import statements. The classpath variable is used to locate packages and classes for compilation and execution.
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
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
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
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