Uipoka’s Weblog

Java Packages

with 2 comments

Java Packages
Java package is a collection of java classes, interfaces and subpackages. Its an encapsualtion mechanism that can be used to group related classes, interfaces and subpackages.


From the figure there is a package called rishan contains two other packages: maiden and heaven.The maiden has a class called Metallica that implements an interface called IronMaiden, also found in the same package.In addition, the package maiden has a class called DreamOfMirrors and a subpackage called virtualXI containing a class called ComoEstais. The package heaven has two classes called DreamOfMirrors and BloodBrothers. The class BloodBrothers is a subclass of the class ComoEstais which is found in the subpackage of virtualXI in the package maiden.

Ok the introduction of the package hierarchy is complete.Now lets see how can we define them in our codes.The dot(.) notation is used to uniquely identify package members in the package hierarchy. The class rishan.maiden.DreamOfMirrors are different from the class rishan.heaven.DreamOfMirrors. Most important thing is this figure tells the organization of packages in conceptually and java source code is of no consequences in this regard.The java source code can contain zero of more definitions of classes and interfaces, but the compiler produces a separate class file containig the Java Bytecode for each of them . In java code package declaration tells the compiler that the bytecode be placed in a particular package.

There are two keywords used for package control:
import : Statement to import the java packages or classes into the code.
package: Specifies to which package all classes in a source file belong.

Package declaration:
package [package name];
This declaration must be the first statement in the sourcecode file.
Code example:

package rishan.maiden;

Import declaration:
import [type name]
This declaration is for single class or interface.
import [package name].*;
This declaration import all the classes and interfaces from the specified package.
Import declaration must be the first statement after any package declaration.
Code example:

import rishan.maiden.DreamOfMirrors;
import rishan.maiden.*;

Our working directory is: D:\rishan’s treasure>_

For the DreamOfMirrors class in package rishan.maiden here goes the sample code:
DreamOfMirrors.java

package rishan.maiden;

public class DreamOfMirrors {

	public static void main(String [] ui){
		System.out.println("This is the main class for class Dream of mirrors");	
	}    
}

Compile it using the command: D:\rishan’s treasure>javac DreamOfMirrors.java

Compiler create a bytecode file named DreamOfMirrors.class in the current directory.

Execute the command: D:\rishan’s treasure>java rishan.maiden.DreamOfMirrors

This error occurs:

Exception in thread “main” java.lang.NoClassDefFoundError: rishan/maiden/DreamOfMirrors

What happened here? Java command search the class file from this path ./rishan/maiden/DreamOfMirrors.class.
But there is no rishan folder in the current directory and so for maiden. First we would do that by hand to hand process.Make a directory named rishan in the current directory, then make a directory in rishan named maiden and then copy the class file DreamOfMirrors.class to maiden folder
and run the command:

D:\rishan’s treasure>java rishan.maiden.DreamOfMirrors

and get this output:

This is the main class for class Dream of mirrors

Ahh… at last we have done it successfully .

If you go to ./rishan/maiden and try to run the DreamOfMirrors then again you will get Exception.

There is a way to create the package folders in the time of compilation:

D:\rishan’s treasure>javac -d . DreamOfMirrors.java

In this command –d for destination and dot(.) is for current directory.This command will create ./rishan/maiden (and any other subdirectories if required) under the current directory and place the byte code for all the classes and interfaces in the directories corresponding to the package names.

Lets see the sample codes :
IronMaiden.java

package rishan.maiden;

public interface IronMaiden {
    void sing();
    void concert();
}

Metallica.java

package rishan.maiden;

public class Metallica implements IronMaiden{
    
    public void sing(){
    }
    
    public void concert(){	
    }   
}

ComoEstais.java

package rishan.maiden.virtualXI;

public class ComoEstais {

    public void hello(){
    	System.out.println("Hello method from Como Estais class");
    }
    
    public static void hi(){
    	System.out.println("Como Esatais Amigo :)");
    }
    
} 

BloodBrothers.java

package rishan.heaven;

import rishan.maiden.virtualXI.*;

public class BloodBrothers extends ComoEstais{
	
	    public static void main(String [] ui){
	    	rishan.maiden.virtualXI.ComoEstais.hi();			                  //Full class name is must
	    	rishan.heaven.BloodBrothers obj = new rishan.heaven.BloodBrothers(); //Full class name is must
	    	obj.hello();
	    }
    
}

Use this command:

D:\rishan’s treasure>javac -d . *.java

After successful exucution of the previous command use this command:

D:\rishan’s treasure>java rishan.heaven.BloodBrothers


Output:

Como Esatais Amigo 🙂
Hello method from Como Estais class

If you have any question then leave a comment, i will answer…………….

Advertisements

Written by uipoka

August 22, 2008 at 6:03 am

Posted in Java

2 Responses

Subscribe to comments with RSS.

  1. Rishan go ahead…………………

    Nazmul

    August 25, 2008 at 7:11 pm

  2. Good site though im not good at this.Anyway just go nd dont quit.Ur gonna b d winner.

    Shiplu

    March 30, 2010 at 10:49 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: