Core Java - SPLessons
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 [java]package splesson; public class Simple{ public static void main(String args[]){ System.out.println("Hello, Welcome to package"); } } [/java] With out any IDE package will be compiled as follows. [java]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.[/java] Code will be run as follows. [java]java splesson.Simple[/java] Output Now compile the code result will be as follows. [java]Hello, Welcome to package[/java] 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 [java]package pack; public class A{ public void msg(){ System.out.println("Welcome To Splessons");} } [/java] Now the developer is going to create main method in splesson package with different class. B.java [java]package splesson; import pack.*;//here used import keyword. public class B { public static void main(String args[]){ A obj = new A(); obj.msg(); } } [/java] Here the developer user previous package by using import keyword. [java]import pack.*;[/java] Output When compile the code result will be as follows. [java] Welcome To Splessons [/java]

By using import package.classname;

B.java [java]package splesson; import pack.A; public class B { public static void main(String args[]){ A obj = new A(); obj.msg(); } } [/java] Where the developer used import pack.A; Output When compile the code result will be as follows. [java] Welcome To Splessons [/java]

fully qualified name

B.java [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(); } } [/java] Following is the fully qualified name. [java]pack.A obj = new pack.A();[/java] Output When compile the code result will be as follows. [java] Welcome To Splessons [/java]

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. [java] 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"); } } [/java] 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. [java] Hi John Welcome To Splessons [/java] 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.