Package in java :
package can be defined as a group of similar types of classes, interface, enumeration or sub-package.. Or
Package contains a set of classes and interfaces having unique names.
Benefits of using package in java:
1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
4) This packages can be provide reusability of code.
5) We can create our own package or extend already available package.
Types of packages:
• java API packages. (Built in packages).
• user defined packages
Java API packages
Java API provides a large number of classes grouped into different packages according to functionality. Most
of
the time we use the packages available with the Java API.
Built-in Packages
These packages consist of a large number of classes which are a part of Java API.
Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math
operations).
This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user interfaces (like button ,
;menus etc).
6) java.net: Contain classes for supporting networking operations.
User defined packages :
To create a package, put keyword package at the top of a java source file .
• we should declare the name of the packages using package keyword and followed by a package name
Syntax: package package_name;
E x: - package pack1;
How to Create a package: Creating a package in java is quite easy. Simply include a package command
followed by name of the package as the first statement in java source file.
package mypackage;
public class student
{
Statement;
,}
The above statement will create a package name mypackage in the project directory.Java uses file system
directories to store packages.
For example the .java file for any class you define to be part of mypackage package must be stored in a
directory called mypackage.
Additional points about package:
• A package is always defined as a separate folder having the same name as the package name.
• Store all the classes in that package folder.
• All classes of the package which we wish to access outside the package must be declared public.
• All classes within the package must have the package statement as its first line.
• All classes of the package must be compiled before use (So that they are error free)
Example of Java packages:
Package mypack;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
How to compile and run java package program:
You need to use fully qualified name e.g. mypack.Simple etc to run the class.
To Compile: javac -d . Simple.java
To Run: java mypack.Simple
Output: Welcome to package
The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The .
represents the current folder.
Subpackage OR Nested package in Java
A package inside a package is called a subpackage or Nested package. It should be created to categorize the
package further.
Let's take an example, Sun Microsystems has defined a package named java that contains many classes
like System, String, Reader, Writer, Socket, etc. These classes represent a particular group. For example,
Reader and Writer classes are for Input/Output operations, Socket and ServerSocket classes are for networking,
etc. and so on.
So, Sun has subcategorized the Java package into subpackages such as lang, net, io, etc. and put the
Input/Output related classes in the io package, Server and ServerSocket classes in the net package and so on.
The standard for defining a package is domain.company.package for examlle com.tpointtech.bean or
org.sssit.dao.
Example of Nestedpackage
Example
1.package com.tpointtech.core;
2.class Main {
3. public static void main(String args[]){
, 4. System.out.println("Hello nestedpackage");
5. }
6.}
To Compile: javac -d . Main.java
To Run: java com.tpointtech.core.Main
Output:
Hello nestedpackage
Importing classes from packages
In java, the import keyword used to import built-in and user-defined packages. When a package has imported,
we can refer to all the classes of that package using their name directly.
The import statement must be after the package statement, and before any other statement.
Using an import statement, we may import a specific class or all the classes from a package.
🔔 Using one import statement, we may import only one package or a class.
🔔 Using an import statement, we can not import a class directly, but it must be a part of a package.
🔔 A program may contain any number of import statements.
How to import the packages
1.. import package.*;// Importing All Classes from a Package
2. import package.classname;// Importing a Single Class
3. fully qualified name. //import specific class
1.import all classes using import package.*
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.
Syntax:
import packageName.*;
Example
import java.util.*;
public class MyApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
list.add("Java");
System.out.println("List: " + list);
}
}
2. Importing a single class Using Packagename.classname
If you import package.classname then only declared class of this package will be accessible.
Syntax: import packageName.ClassName;
package can be defined as a group of similar types of classes, interface, enumeration or sub-package.. Or
Package contains a set of classes and interfaces having unique names.
Benefits of using package in java:
1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
4) This packages can be provide reusability of code.
5) We can create our own package or extend already available package.
Types of packages:
• java API packages. (Built in packages).
• user defined packages
Java API packages
Java API provides a large number of classes grouped into different packages according to functionality. Most
of
the time we use the packages available with the Java API.
Built-in Packages
These packages consist of a large number of classes which are a part of Java API.
Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math
operations).
This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user interfaces (like button ,
;menus etc).
6) java.net: Contain classes for supporting networking operations.
User defined packages :
To create a package, put keyword package at the top of a java source file .
• we should declare the name of the packages using package keyword and followed by a package name
Syntax: package package_name;
E x: - package pack1;
How to Create a package: Creating a package in java is quite easy. Simply include a package command
followed by name of the package as the first statement in java source file.
package mypackage;
public class student
{
Statement;
,}
The above statement will create a package name mypackage in the project directory.Java uses file system
directories to store packages.
For example the .java file for any class you define to be part of mypackage package must be stored in a
directory called mypackage.
Additional points about package:
• A package is always defined as a separate folder having the same name as the package name.
• Store all the classes in that package folder.
• All classes of the package which we wish to access outside the package must be declared public.
• All classes within the package must have the package statement as its first line.
• All classes of the package must be compiled before use (So that they are error free)
Example of Java packages:
Package mypack;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
How to compile and run java package program:
You need to use fully qualified name e.g. mypack.Simple etc to run the class.
To Compile: javac -d . Simple.java
To Run: java mypack.Simple
Output: Welcome to package
The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The .
represents the current folder.
Subpackage OR Nested package in Java
A package inside a package is called a subpackage or Nested package. It should be created to categorize the
package further.
Let's take an example, Sun Microsystems has defined a package named java that contains many classes
like System, String, Reader, Writer, Socket, etc. These classes represent a particular group. For example,
Reader and Writer classes are for Input/Output operations, Socket and ServerSocket classes are for networking,
etc. and so on.
So, Sun has subcategorized the Java package into subpackages such as lang, net, io, etc. and put the
Input/Output related classes in the io package, Server and ServerSocket classes in the net package and so on.
The standard for defining a package is domain.company.package for examlle com.tpointtech.bean or
org.sssit.dao.
Example of Nestedpackage
Example
1.package com.tpointtech.core;
2.class Main {
3. public static void main(String args[]){
, 4. System.out.println("Hello nestedpackage");
5. }
6.}
To Compile: javac -d . Main.java
To Run: java com.tpointtech.core.Main
Output:
Hello nestedpackage
Importing classes from packages
In java, the import keyword used to import built-in and user-defined packages. When a package has imported,
we can refer to all the classes of that package using their name directly.
The import statement must be after the package statement, and before any other statement.
Using an import statement, we may import a specific class or all the classes from a package.
🔔 Using one import statement, we may import only one package or a class.
🔔 Using an import statement, we can not import a class directly, but it must be a part of a package.
🔔 A program may contain any number of import statements.
How to import the packages
1.. import package.*;// Importing All Classes from a Package
2. import package.classname;// Importing a Single Class
3. fully qualified name. //import specific class
1.import all classes using import package.*
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.
Syntax:
import packageName.*;
Example
import java.util.*;
public class MyApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
list.add("Java");
System.out.println("List: " + list);
}
}
2. Importing a single class Using Packagename.classname
If you import package.classname then only declared class of this package will be accessible.
Syntax: import packageName.ClassName;