Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction:
2.
Advantages of packages
3.
Accessing a package
4.
FAQs:
5.
Key Takeaways: 
Last Updated: Mar 27, 2024

Introduction to Packages

Author Nishant Rana
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction:

Packages can be considered as folders in the directory. They are used to store the classes, interfaces, and sub-packages of similar types. They are the main part of the encapsulation process as they help in encapsulating the data of the same type. There are two types of packages:-

  1. User-defined packages: The packages which are made by the users are known as user-defined packages.
  2. Built-in packages: The packages which are already present in java are known as built-in packages. For example, util, io, lang, etc.

Also see, Duck Number in Java and Hashcode Method in Java.

Advantages of packages

  1. They help in encapsulating similar classes and interfaces.
  2. They help in avoiding the errors caused due same names of the classes.
  3. It makes searching and usage of the classes, interfaces, and sub-packages easy.
  4. It also provides security by not allowing unauthorized access. The private or protected variables, classes can not be accessed outside the same package.

 

We generally use the package keyword to make a package as follows:-

package CodingNinjas;

public class Main{
    public static void main(String args[]){  
        System.out.println("Hey..");
    }  
}

 

We need to run the following command to compile any java code:

javac -d directory javafilename  

 

For the above code we need to write:-

javac -d . Main.java  

 

After running the above command a class named Main will be made under CodingNinja package.

If you are using a Java code editor then we can directly create a package using that code editor.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Accessing a package

We can access the package using three ways:-

  1. packageName.*: When we use this method, then all the class in that package gets imported to the current class. Refer to the below code:
package example
import CodingNinjas.*;

public class Main{
    public static void main(String args[]){  
        System.out.println("Hey..");
    }  
}

In the above code, we can access all the classes of the CodingNinjas package.

2. packagedName.className: When we use this method then only a particular class of that package gets imported. Refer to the below code:

package example
import CodingNinjas.Main;

public class Main{
    public static void main(String args[]){  
        System.out.println("Hey..");
    }  
}

In the above code, only the Main class of the CodingNinjas package gets imported.


3. packageName: If we use this method then all the declared classes of that package get imported. In this method, there is no need to write import because we can access its classes by using packageName.className. It is generally used when the two packages have classes with the same name. Refer to the below code:

package example

public class Main{
    public static void main(String args[]){  
        
        //Creating an object of the Main class of CodingNinjas package.
        CodingNinjas.Main obj = new CodingNinjas.Main();
    }  
}

 

The order to write a program in java must be:-

Also see, Swap Function in Java

FAQs:

  1. What is a Package?
    • Packages can be considered as folders in the directory. They are used to store the classes, interfaces, and sub-packages of similar types.
  2. List different types of methods to access a package?
    • We can access the package using three methods:
      1. packageName.*
      2. packageName.className
      3. packageName

Key Takeaways: 

In this blog, we have covered the following things:

  1. We first discussed what are packages and what are its advantages.
  2. Then we discussed how to access a package.

 

If you want to learn more about Java and want to practice some questions which require you to take your basic knowledge on Java a notch higher, then you can visit our Guided Path for Java

 

Until then, All the best for your future endeavors, and Keep Coding.

 

 

Next article
Java Applet
Live masterclass