Hi there,

I am Rohan!

""""✰I am always blown away to see thought leaders like Rohan with ambition, having a positive impact on the world!"✰ Michael Walmsley, CEO and founder of CodeAvengers.
Join Rohan Chaubey on

Packages In Java!

In our previous posts we covered about Characteristics of Java, How to install Java, Classes, Objects, White Spaces, Data Types, Variables, Array Of Objects, Method overriding, Constructor Overloading, Scanner class, Inheritance, Interfaces, Abstract Classes and Access Specifiers... Click here to read the previous Post!

In this post, we are going to learn what are packages. 

Ever heard about packages in general English ? What are packages ? In simple English packages are nothing but a box or an container in which some objects or group of objects are stored.

Similarly, In java, A package is nothing but collection of classes and interfaces. The declaration of a package resides at the top of a java code(source file). A simple package looks as shown in the box below :

package package_name;
public class_in_package

What if you want to create a sub package? As we can create a sub folder in our computer system, similarly we can also create a sub package by using the following statement : 

package rootPackage.subPackage1;

How to use a class which you created inside a package?

The class cannot be accessed by using the class name  alone. You need to precede the package name. For this purpose "import" keyword is used. You can give any number of import statements.

Importing Cases :

Case #1 : 
To import a single class, use the following statement :

import java.awt.Rectangle;

Case #2 : 
To import all classes in a package, use the following statement :

import java.awt.*;

Case #3 : 
To import all the static members of the class, use the following statements :

import static packagename_Class_name.*;

Now that you know how packages work, lets start implementing them in our programs... Click Here For Programs On Packages!

Happy Learning. Have a great day! 

ABOUTRohan Chaubey

Rohan Chaubey is the founder of “Be Rohanlicious” – a blog that teaches you to be smart with social media, blogging and programming.

Rohan is on a mission to help all the enthusiastic souls like you, who want to “make it to the next level” and stand out of the crowd.

His e-book “Make People Want You” is releasing soon. Allow him to inject awesomeness in your life by signing up to his list here.