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 :
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 :
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 :
Case #2 :
To import all classes in a package, use the following statement :
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!
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.