Java Packages
In Java, a package is a collection of related classes, interfaces, and sub-packages. It provides a way to organize and structure code in a hierarchical manner, which makes it easier to manage and reuse code.
Java provides a number of predefined packages, such as java.lang, java.util, and java.io, among others. These packages contain a set of classes and interfaces that are commonly used in Java applications.
In addition to the predefined packages, Java allows you to create your own custom packages. To create a package, you need to use the package keyword at the beginning of your Java source file. For example, if you want to create a package called "myPackage", you can use the following syntax at the beginning of your Java file:
package myPackage;
Once you have created a package, you can place your Java files inside that package by including the package statement at the beginning of each file. For example, if you have a class called "MyClass" that belongs to the "myPackage" package, you can define it as follows:
package myPackage;
public class MyClass {
// class definition goes here
}
To use a class or interface from a package, you need to import it into your Java source file using the import keyword. For example, if you want to use the Math class from the java.lang package, you can import it as follows:
import java.lang.Math;
public class MyClass {
public static void main(String[] args) {
double x = Math.sqrt(25);
System.out.println("Square root of 25 is " + x);
}
}
In the above example, we imported the Math class from the java.lang package and used its sqrt() method to calculate the square root of 25.
Package are used to grouping of classes, interfaces and sub packages. That is very useful mechanism to arrange well code structures under the protection. Package is like a namespace which are resolve ambiguity of scopes. Because there can be possible exist similar name classes in two different package.
Define custom package
package packageName;
package name start with lowercase character.
package testpack;
public class Test{
//main method of this package
public static void main(String args[]){
System.out.println("Test Package");
}
}
//File Test.java
//Compile
//javac -d . Test.java
//Run
//java testpack.Test
Test Package
When compiling this source code, that is create a subfolder on root application that name is testpack and inside this folder would have Test.class file.
Use package
import keyword are used to utilize the custom or inbuilt package in java source code file. this statement are normally defines at top of the source code.
package sets;
public class Record{
//Method of Record class
public void test(){
System.out.println("Start Test");
}
public void operation(){
System.out.println("Start Operation");
}
}
//File Record.java
//Complie
//javac -d . Record.java
//Import package class
import sets.Record;
class Student{
public static void main(String[] args) {
Record obj = new Record();
obj.test();
obj.operation();
}
}
//File: Student.java
//Compile
//javac Student.java
//run java Student
Start Test
Start Operation
Import an entire package
To import an entire package in Java, you can use the * wildcard character in the import statement. For example, to import all the classes and interfaces from the java.util package, you can use the following import statement:
import java.util.*;
This will import all the classes and interfaces from the java.util package, which you can then use in your Java program without having to specify the package name for each individual class or interface.
However, it is generally recommended to import only the specific classes and interfaces that you need from a package, rather than importing the entire package. This helps to keep your code more organized and reduces the chance of naming conflicts. It also makes it easier to understand which classes and interfaces are being used in your code.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment