Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday, 15 June 2017

Java Package

 Package

Java package is a collection of similar types of classes, interfaces and sub-packages. See the diagram below


Java Package

Advantage of Java Package

  • The first advantage is that, easy to use and easy to search.
  • Java package helps to categorize the classes and interfaces so that we can easily search or maintain the classes and interfaces.
  • Java package reduce the application time because the by the help of package we can reuse the code.
  • Java package provides access protection.
  • Java package removes naming collision.
  • Java package improves the performance of application.

Types of Package

There are two types of packages in java, these are given below
  1. Predefined or built-in packages.
  2. User defined packages.

1) Predefined or built-in packages : These packages are already defined by the company like oracle or sun microsystem as java api. Here we have may predefined or built-in or ready made  packages in java which contains sub packages, classes and interfaces. Predefined packages are
  • java.lang.*;
  • java.io.*;
  • java.util.*;
  • java.sql.*;
  • java.text.*;

2) User-defined packages : User defined package is a package which is designed by the user, in other word the package we create is called user defined package in java.


Java Package Example

This is a simple example of user defined package or java package. Here we will use 'package' keyword to create a package in java.

package first;
public class Test
{
public static void main(String args[])
{
System.out.println("package created");
}
}

save file : Test.java
compile : javac -d . Test.java
run : java first.Test

output : package created

Explanation of above program : 

(1) First of all we have used package keyword to create a package in java.

for example : package first

(2) Second we compiled the java package program by the help of javac -d . Test.java.
  • Here javac is a tool which is used to compile the java source code.
  • -d , the -d is a switch that tells the compiler where to put the class file i. e it represents the destination.
  • ' . ' (dot) represents the current folder.
  • After .(dot) we used our java file name which is saved by Test.java
(3) After compiling java package program we will run program by using java first.Test.
  • Here java is tool which is used to run or execute the java source code.
  • If we want to run our java package program than we have to use package name with class name . for example java  first.Test. 


How to access package from another package

There are 3 ways you can access package from the outside of package.
  • import package.*;
  • import package.classname;
  • fully qualified name.
1) using import package

In this case, If we use package.* then all the classes and interfaces of this package will be accessible but not sub-package.

Here we will use the import keyword which makes the classes and interfaces accessible of another package to the current package.

package first;    //save by A.java
public class A
{
public void show()
{
System.out.println("welcome");
}
}

package second;
import first.*;
class B   save by B.java
{
public static void main(String args[])
{
A a =
new A();
a.show();
}
}

output : Hello

2) Using import package.classname.*;

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

package first;    //save by A.java
public class A
{
public void show()
{
System.out.println("welcome");
}
}

package second;
import first.A;
class B   save by B.java
{
public static void main(String args[])
{
A a = 
new A();
a.show();
}
}

output : welcome

3) Using fully qualified name

Now in this case, there is no need to import keyword. If you use fully qualified name then only declared class of this package will be accessible.


package first;    //save by A.java
public class A
{
public void show()
{
System.out.println("welcome");
}
}

package second;
class B   save by B.java
{
public static void main(String args[])
{
first.A a = 
new first.A();//using fully qualified name
a.show();
}

}

output : welcome


How to Create Sub-Package in Java Package

If one package contains another package is called sub-package in java. In java we have many packages and their sub-packages like java is a package and lang, util, awt, text, sql is a sub-packages of java package.

package com.javatutorial.core;
public class Simple
{
public static void main(String args[])
{
System.out.println("sub-package created");
}
}

compile : javac -d . Simple.java
run : java com.javatutorial.core.Simple

output : sub-package created

you will see, com->javatutorial->core packages are created where you saved your file and .class file will be available in core package i.e com.javatutorial.core package.


Some Rules In Java Package

There are some rule we have to keep in mind, these are
  • Package making command must be the first line in any java file.
  • All the import statement must be written above the class.
  • The modifiers of class, methods and constructor which is available in the package must be public.

Share:

6 comments:

  1. Great Article android based projects

    Java Training in Chennai

    Project Center in Chennai

    Java Training in Chennai

    projects for cse

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete
  2. Hi, I read your whole blog. This is very nice. Good to know about the career in qa automation is broad in future. We are also providing various Java Training, anyone interested can Java Training Online for making their career in this field .

    ReplyDelete
  3. Thanks for this post. It proves very informative for me. Great post to read. Visit my website to get best Information About Best MPSC coaching Centre in Borivali.
    Best MPSC coaching Centre in Borivali
    Top MPSC coaching Center in Borivali

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate