Saturday, August 25, 2012

What is package in Java - How to use package - example tutorial

What is package in Java
package in Java is a way to organize related functionality in single place. If you look on File System package in Java represent a directory where Java source file is stored before compilation and class files are stored after compilation. For example if you create a class HelloWorld in a package called com.java67.welcome; than it will reside under directory com/java67/welcome in source tree and you can view that in your IDE like Eclipse or Netbeans or even by navigating to file system. once you compile your Java program either by using your IDE, Ant build Script or maven compile plugin; it creates class files under same package structure. For example maven will create target/classes/com/java67/welcome directory and place HelloWorld.class inside that. Its mandatory that class files reside on same package or directory as declared in there source file using package keyword, failure to do will result in java.lang.NoClassDefFoundError in Java. In summary answer of question What is package in Java can be as simple as that package is a keyword in Java which is used to specify directory structure for particular class file.



How to create package in Java

If you are using IDE like Eclipse for developing your Java program than you don't need to do anything. Just click on new-->package and Eclipse will ask you name of the package, put name of the package and you are done. Now if you want to create Java class on that package, just select  the package in package explorer and create new-->Java Class. If you are not using any IDE than you manually need to create directories corresponding to package in Java.

Predefined package in Java
All the java classes are structured in to different package based upon there functionality. for example java.lang package contains classes which are essential to Java programming language e.g. Thread, Exception Error, Object etc. On the other hand package like java.util contains all utility classes e.g. Collection classes, Scanner and other utility.  java.io package contains Java classes related to Input and Output functionality. java.util.concurrent also called sub package of java.util contains Concurrent utility classes like CountDownLatch, CyclicBarrier, Semaphore etc

Why use package in Java
What is package in Java cerate example tutorial
package provides Encapsulation in Java program. default access modifier for any variable or class is package-private i.e. they are only visible into package, on which they are declared. By using package you Encapsulate whole functionality which allows you to change the functionality, include new functionality or just change the implementation without breaking whole application. Though package is not the highest degree of Encapsulation in Java which is achieved using private keyword, it still second best option and must to encapsulate whole functionality rather than just a class.

How to use package in Java
Using package in Java is very simple. just use package keyword along with name of package at top of your Java source file to declare package in Java. package declaration must be first line in Java source file even before import statement. here is an example of how to use package in java

package blog.java67.welcome

public class Hello{
    public static void main(String args[]){
         System.out.println("An Example of using package in Java");
    }

}

That's all on What is package in Java, Why should we use package in Java and how to use package in Java. Simple example of package as shown in above Java program is enough to make you going and explores how Java source file and class file organize into package.


Other Java tutorials from java67 blog
Difference between TreeSet and HashSet in Java


1 comment:

  1. Great post, just to add here are answers of some frequently asked questions about package in Java :

    1) java.lang package is automatically imported in every Java class.

    2) You can import all classes and sub packages from a package by using wildcard * e.g. import com.abc.* will import all classes on package com.abc as well as classes on any sub package.

    3) package must be first statement, even before import statement in Java class file.

    ReplyDelete

Java67 Headline Animator