Core Java - SPLessons

Java Packages

Chapter 28

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Packages

Java Packages

shape Description

Java Packages, The package can be defined as group of classes, interfaces , sub-packages. Packages are divided into two types they are built-in packages and user defines Java Packages. The advantage of the package is that, it provides access protection, removes naming collision, class and interfaces can be managed easily. While doing any Java program the regularly using packages are java.lang, java.io packages, where java.lang package will consist set of fundamental classes and java.io packge will consist set of input, output streams.

shape Conceptual
Figure

Lang package contains the classes that are basic fundamental to the formation of the Java programming language.
Util package contains the collections framework, legacy collection classes, event model, date and time classes etc.
AWT package provides the packages to develop GUI application.

For more detailed overview on collections click here .

Creating A Package

shape Description

Java Packages, Package is a keyword, by using this keyword one can create another package. Following is an example where developer is going to create the Splesson package.
Simple.java

package splesson;

public class Simple{  
	 public static void main(String args[]){  
	    System.out.println("Hello, Welcome to package");  
	   }  
	}  

With out any IDE package will be compiled as follows.

javac -d . Simple.java

//The -d is a switch that tells the compiler where to put the class file i.e. it represents destination folder.

Code will be run as follows.

java splesson.Simple

Output
Now compile the code result will be as follows.

Hello, Welcome to package

By using three ways developer can access the Java Packages from another package, they are as follows.

Following is the structure to use packages.

By using import package.*;
Here developer is going create another package with the name pack.
A.java

package pack;  
public class A{  
  public void msg(){
	  System.out.println("Welcome To Splessons");}  
}  

Now the developer is going to create main method in splesson package with different class.
B.java

package splesson;
import pack.*;//here used import keyword.

public class B {
	public static void main(String args[]){  
		   A obj = new A();  
		   obj.msg();  
		  }  
}

Here the developer user previous package by using import keyword.

import pack.*;

Output
When compile the code result will be as follows.

Welcome To Splessons

By using import package.classname;

B.java

package splesson;

import pack.A;

public class B {
	public static void main(String args[]){  
		   A obj = new A();  
		   obj.msg();  
		  }  
}

Where the developer used import pack.A;
Output
When compile the code result will be as follows.

Welcome To Splessons

fully qualified name

B.java

package splesson;

import pack.A;

public class B {
	public static void main(String args[]){  
		pack.A obj = new pack.A();//fully qualified name.
		   obj.msg();  
		  }  
}

Following is the fully qualified name.

pack.A obj = new pack.A();

Output
When compile the code result will be as follows.

Welcome To Splessons

Java Static Import

shape Description

The static import is the good feature of Java 5 that helps the java programmer to authenticate any static member of a class. The use of Static Import is to reduce the length of the code if user authenticate any static member of class. The following is an example.

package com;

import static java.lang.System.*;    
class StaticImport{  
  public static void main(String args[]){  
     
   out.println("Hi John");//Now no need of System.out  
   out.println("Welcome To Splessons");  
  
 }   
}   

In the above scenario, System.out.println is not used instead of that only out.println has used. Now compile the code result will be as follows.

Hi John
Welcome To Splessons

Both are used to display the output. But system.out.println is used in java and out.prinln is used in scripting languages

Summary

shape Key Points

  • In Java Packages, one can create sub package.
  • java.io is used to give input, output streams.