Recent

14.Package in Java

Package In Java


A package in Java is a collection of similar type of classes and interfaces. These are similar to headers file in C language.
Advantages of packages:
·         Easy to use (we can find similar type classes and interfaces on a same place, so it makes our work easier).
·         They are used for linking classes to otherprograms(to increase performance). And to link a package to a Java programs Java has given a keyword “import”.
***************************_*****************************
How to create a package:
To create a package Java has given a keyword “package”. We can create a package by using package packagename statement before defining a class.
Simple program to create a package:


package mypackage; //package defined withmypackage name

class Demo
{
void show()
{
System.out.println("Show");
}

public static void main(String[] args)
{
Demo d = new Demo();
d.show();
}}
Now to create a package we need to use a different command to compile the program. The following command is used to compile a program to create a package.
———>> javac     -d     destinationdirectory     filename.java
and to run the program, set path fordestinationdirectory and write the following command
———>> java      packagename.filename
destinationdirectory is the destination where we want our package to be created. And if we want to save our program in the current directory then we can use . (Dot) in place of destination directory.
See the output of the above program for picture:

***************************_*****************************
Keep more than one classes in single package:
To keep more than one classes in single package we can do these things:
1.    keep separate Java file for each class and each class in every file should be public.
2.    Also keep same package making command in each file.
See this example to understand this:


package mypackage;

public class First
{
void show()
{
System.out.println("I am from class First");
}
public static void main(String[] args)
{
First b = new First();
b.show();
}}



package mypackage;

public class Second
{
void show()
{
System.out.println("I am from class Second");
}
public static void main(String[] args)
{
Second b = new Second();
b.show();
}}



package mypackage;

public class Third
{
void show()
{
System.out.println("I am from class Third");
}
public static void main(String[] args)
{
Third b = new Third();
b.show();
}}
Save these files as First.java, Second.java and Third.java respectively. and run as in the picture. I usedjavac    -d    .    *.java to compile the programs. In this command . (Dot) is used to create package in current directory and *.java is used to compile all the .java files in a directory together.


 ***************************_*****************************
How to use a package in another program
To use a package into another Java program, Java has given a keyword “import”. In Java all the packages are found in rt.jar file and the classpathfor this file is set by default in Java.
So, to use all those packages which are not found into the rt.jar file, we have to set the classpath of those packages manually.
Consider the programs:


package basicpackage;  //packagebasicpackage defined

public class Temp
{
public void show()
{
System.out.println("show from basic package");
}

public static void main(String[] args)
{
Temp t = new Temp();
t.show();
}}



package secondpackage;  //packagesecondpackage defined

import basicpackage.*;  //basicpackageimported

public class Demo
{

public static void main(String[] args)
{
Temp t = new Temp();
t.show();  //show method from basicpackagewill run here
}}
Output:
***************************_******************************
What if the imported package is not in the currentdirectory(its class path is not set):
To use all those packages which are not found into the rt.jar file or in current directory, we have to set the classpath of those packages manually. Here is how we will do it:
In this program we have a Java file (Demo.java) in C:\java directory whose package will be used in Temp.java program which is present in E:\java programs\javacp.com directory.


//Temp.java saved in C:\java

package basicpackage;

public class Temp
{

public void show()
{
System.out.println("show from basic package");
}

public static void main(String[] args)
{
Temp t = new Temp();
t.show();
}}



//Demo.java saved in E:\java programs\javacp.com

package secondpackage;

import basicpackage.*;

class Demo
{
public static void main(String[] args)
{
Temp t = new Temp();
t.show();
}}
Temp.java will compile successfully here but Temp.java will give an error “basicpackage not found”. Now, for basicpackage to use in Demo.java which is in some other directory we need to set the class path of basicpackage manually. See the pictures to understand it better.
1.    Compilation of both files without setting a class path for basicpackage.
2.    After setting the class path and compile we will have two packages created in different folders.
3.    Setting the class path and running programs. We can see that show function is used frombasicpackage.

***************************_******************************
How to import only static things of a package:
If we import any class statistically then all the static data members and member function of this class can be used directly (without using a class name) into the class of another package.
If we import the class in another class statistically then we cannot use the non static things of that class which is imported. If we want to use the non static things too then we can use non static import along with the static import.
Syntax to import only static things of a package: import staticpackagename.classname.*;
An Example of static import:


package p1;

public class Demo
{
public static int x =90;

public static void show()
{
System.out.println("Show from static show in package p1");
}

public void show1()
{
System.out.println("Show from non staticshow function");
}

public static void main(String[] args)
{
new Demo().show();
}
}



package p2;  //package p2 defined

//imported static methods and members of Demo class
import static p1.Demo.*;  


class Temp
{
static int x = 420;

public static void main(String... s)
{
show();  //accessing the show without any object and classname
System.out.println(x);  
}
}
Output:
***************************_******************************
Sub-package in Java
It is nesting of packages. we can define a package inside other package.
Example:


packageone.two.three.andsoon;  //subpackagesdefined

//two inside one, three inside two, andsooninside three

public class Demo
{

void show()
{
System.out.println("Hello");
}

public static void main(String[] args)
{
Demo d = new Demo();
d.show();
}
}
Output:
***************************_******************************
Some Important Questions
Question 1: what is a package?
Answer: A package in Java is a collection of similar type of class and interfaces.
Question 2: Is it possible that a class do not have any package in Java?
Answer: No,
·         No class exists in java without package.
·         If you have not given the package making command into the .java file then the compiler will create adefault package and put the .class file in this package.
Question 3: What is the name of default package in Java?
Answer: Default packages does not have any name in Java.
***************************_******************************

No comments:

Post a Comment

Bug-dbug Designed by Bugdbug - Developed by Belson Raja Copyright © 2016

Theme images by Bim. Powered by Blogger.
Published By Bugdbug India