In this blog, we are telling about the What are the Java Packages
What is Package In Java?
It is the collection of interfaces, subclasses, and classes. It organizes our classes into the folder structure so that we quickly locate our files and when we want to need we can use the file. It also increases the code reusability in our program
Every package in java has a unique name through which we can organize the classes and interface into the namespace, name group
Interfaces and classes which have a similar name cannot use in the same package so they are used in different packages. This is possible only when we are using a different namespace and also know about What are the Java Packages
The syntax for the package:
package nameOfPackage;
How to Create the Package and What are the Java Packages
Creating the package is very simple to let’s follow the steps:
- Firstly choose the name of the package
- Then add the package command as the first line of code in the java source file
- That source file contains the interfaces and classes that you should include in the package
- Compile the java packages
Step 1: Package the program in java
package p1;
class c1(){
public void m1(){
System.out.println("m1 of c1");
}
public static void main(string args[]){
c1 obj = new c1();
obj.m1();
}
}
- To use the package in the class, we define the package in the first line with package p1
- Then create the class c1
- Define the method m1 which prints some line
- Then define the main method
- Create the object of the class
- then call the method m1 and know about the What are the Java Packages
Step 2: Then save the file name demo.java
Step 3:then we compile the file
When the compilation is completed class a is created but there is no package created.
Step 4: For creating the package you have to use this command
javac –d . demo.java
To use this command you have to create the package
The “.” operator shows that you are working in the current directory.
Step 5: When you run the code you should see that it creates the package p1 and in the package, there is the c1.class file know about the What are the Java Packages
Step 6: Run the code with the same code
javac –d .. demo.java
“..” represents the parent’s directory and this file will be saved in the c drive
Step 7: When you create the subpackage p2 of package p1.
package p1.p2;
class c1{
public void m1() {
System.out.println("m1 of c1");
}
}
Step 8: Then compile the file
Then creates the package of p2 having the class c1 inside the package
Step 9: You should see that the package of the name followed by the subpackage in your code
java p1.p2.c1
and it gives the output m1 of c1
Read also: Making a Thumbnail on youtube with iPhone
How to import Package
You need to take a full name for creating an object of the class in your code and know about the What are the Java Packages
For Example, we can use this command:
java.awt.event.actionListner object = new java.awt.event.actionListner();
It seems very low uninteresting to type the long dot-separated package path name for each class whichever you want to use. So it is always recommended you use an import statement.
Syntax of the code:
import packageName;
You can use its class without mentioning its qualified name for importing and also know about the What are the Java Packages
import java.awt.event.*;
import javax.swing.JFrame // here only the JFrame class is imported
//Usage
JFrame f = new JFrame; // without fully qualified name.
Let’s see the example (for importing the package)
Step 1:- Use and run the code into an editor
package p3;
import p1.*;
class c3{
public void m3(){
System.out.println("Method m3 of Class c3");
}
public static void main(String args[]){
c1 obj1 = new c1();
obj1.m1();
}
}
Step 2:- just save it with Demo2.Java Extention. Compile the file by using its name and according to the specified syntax javac –d. Demo2.java.
Step 3:- Run the code using the command java p3.c3.
Advice for packages-
To avoid the naming conflict, packages are given names of the company’s domain name in a back-pedal manner like- com.clawweb.com, com. Infosys.
When you don’t know the exact package name then a class is in the default package (your current directory) and the box itself is no name, then you are able to execute the assignment earlier.
You should be careful during creating a package as you have to statement for creating a package that must be edited before any other import statements.
import package p1.*;
package p3;
//correct syntax
package p3;
import package p1.*;
The java. lang package is imported by default for any class that you create in Java
The java API is vast and contains classes that can perform all your programming tasks, from Data Structure Manipulation to Networking. Remember that you will use API files (so keep this in mind).
FAQ
Name of the default package that is imported by default?
It is by default loaded by JVM. The Java. lang package is always imported automatically.
What do you mean by packages of Java and how it gets used?
A java package is a naming context for interfaces & classes. It is used to create a separate namespace for groups of interfaces & classes. Packages are used to organize related classes & interfaces into a single API unit and also take control over classes & interfaces.
Can I import the same package more than one time?
JVM will load the class internally only once and no matter how many times you will import the package.
Can I say if I import the package then it automatically imports the sub-packages as well?
No, you are required to import the sub-packages as well and you can’t rely on sub-packages while importing main packages.
Is it right that the import checked for reasonableness at compile time?
We can say yes, it gets checked for semantic validity at compile time.