Package in Java





  • Java provides a mechanism for partitioning the class namespace into more manageable chunks. This mechanism is the package.

  • The package is both a naming and a visibility control mechanism.

  • A Java package is a group of co-related types of classes, interfaces and sub-packages.

  • In Java, there are two types of packages :

    1. Built-in package : e.g. java, lang, util,swing, io, sql etc.
    2. User-defined package

  • Defining a Package :

  • To define a package we use keyword package.

  • Simply include a package statement as the very first statement in a Java source file.

  • Any classes declared within that file will belong to the specified package.

  • The package statement defines a namespace in which classes are stored.

    Syntax :

    package packagename;

Finding Packages and CLASSPATH :

As just explained, packages are mirrored by directories.

This raises an important question: How does the Java run-time system know where to look for packages that you create?

The answer has two parts:
First, by default, the Java run-time system uses the current working directory as its starting point. Thus, if your package is in the current directory, or a subdirectory of the current directory, it will be found.
Second, you can specify a directory path or paths by setting the CLASSPATH environmental variable.


Simple example of java package :

//save as Demo.java
package mypack; 
public class Demo{  
 public static void main(String []ar)
  {  
   System.out.println("Package Demo");
  }  
}           

How to compile java package ?

On IDE there will be no issue to compile.
But, if you are not using IDE then write the following command :

javac -d dest-directory sourcefilename

For example :

javac -d . Demo.java
Where :

-d specifies creation of package.

dest-directory specifies the destination where to create the package and put the class.

In the above compilation, i am using . ( dot ), that denotes the current directory, it will create the package at current location and put the class file inside.

You can specify any location, directory, drive to your system, where you want to put your package and class file. Just write the specified path in place of .( dot ).


How to run java package program ?

To run a file that belongs to package, you need to use fully qualified name of class (class-name with package-name).

To Compile : 
javac -d . Demo.java

To Run :
java mypack.Demo
OUTPUT :
Package Demo

How to access package from another package?

There are two ways to access the package from outside the package.

  1. Using import
  2. Using fully qualified name.

1. Using import :

  • Java includes the import statement to bring certain classes, or entire packages, into visibility.

  • Once imported, a class can be referred to directly, using only its name.

  • In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions.

  • There are two ways to use import statement :

    1. import package.*;

      If you import package.* then all the classes and interfaces of this package will be accessible but not subpackages.

    2. import package.classname or interfacename;

      If you import package.classname then only declared class of this package will be accessible.

Exampl :

//save as Demo.java
package pack; 
public class Demo                
{
 public void show()
  {
   System.out.println("Show Method");
  }
}           
//save as Test.java
package mypack;
import pack.Demo;   
public class Test                
{
 public static void main(String []ar)
  {
   Demo d = new Demo();
   d.show();
  }
}           

Compile and Run :

First :
javac -d . Demo.java

Then
javac -d . Test.java

To Run :
java mypack.Test

OUTPUT :
Show Method

2.Using fully qualified name :

Exampl :

//save as Demo.java
package pack; 
public class Demo                
{
 public void show()
  {
   System.out.println("Show Method");
  }
}           
//save as Test.java
package mypack; 
public class Test                
{
 public static void main(String []ar)
  {
   pack.Demo d = new pack.Demo(); 
   d.show();
  }
}           

Subpackage in java

Package inside the package is called the subpackage.

Example :

package com.javaprowess;
public class Demo
{  
 public static void main(String []ar)
 {  
  System.out.println("Subpackage Demo");
 }  
}          
Compile and Run:
javac -d . Demo.java
java com.javaprowess.Demo

OUTPUT:
Subpackage Demo

Send the the class file to another directory or Drive :

Let, your current working directory is Desktop, and you want to save your class file in e:\ drive

TO COMPILE :

Desktop> javac -d e:\ Demo.java
TO RUN :
To run this program from Desktop directory, you need to set classpath of the directory where the class file resides.

Desktop> set classpath=e:\;.;
Desktop> java pack.Demo

run this program by -classpath

Desktop> java -classpath e:\ pack.Demo

static import feature of Java5 :

The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name.

import static java.lang.System.*;
class StaticImportDemo
{  
 public static void main(String []ar)
{
//Now no need of System.out
  out.println("Java");
  out.println("Prowess"); 
 }   
}          

Next topic is access-specifiers




 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved