Java Package
In Java, a package is a group of related types of classes, interfaces, enumerations, and sub-packages.
Packages are used to prevent naming conflicts, and to control access.
Packages are two types:
- Inbuilt Packages
- User-defined Packages
Inbuilt Packages
The existing packages in Java are called as inbuilt packages. Some of the inbuilt packages are Java, lang, io, util, awt, javax, swing, net, and etc.
User-defined Packages
To create a package is quite simple. To include a package command along with the package name of the first statement in the Java source file. That packages contain the classes, interfaces, and annotation types which you can include in the package.
Syntax:
package mypackage;
If a package statement is not used then the class placed to default package, which has no name.
Import a Package
The import keyword helps to access the classes and interface of another package.
- import package.*;
- import package.classname;
Example
import packagename.*;
package calculate; // package name
public class B
{
public void show()
{
System.out.println("Welcome to Onlinetpoint");
}
}
package circle; // package name
import calculate.*; // imported calculate package
public class Importpackage
{
public static void main(String args[])
{
B b = new B();
b.show();
}
}
Example
import package.classname;
package calculate; // package name
public class B
{
public void show()
{
System.out.println("Welcome to Onlinetpoint");
}
}
package circle; // package name
import calculate.B; // imported packagename and class name
public class Importpackage
{
public static void main(String args[])
{
B b = new B();
b.show();
}
}
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com