Package
Java package is a collection of similar types of classes, interfaces and sub-packages. See the diagram below
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
- Predefined or built-in packages.
- 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.
(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 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 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.
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
first.A a = new first.A();//using fully qualified name
a.show();
}
}
output : welcome
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.
Pretty! This was a really wonderful post. Thank you for providing these details.
ReplyDeletecore java training institutes in bangalore
core java training in bangalore
best core java training institutes in bangalore
core java training course content
core java training interview questions
core java training & placement in bangalore
core java training center in bangalore
Great Article android based projects
ReplyDeleteJava 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
Nice and Informative Blog.
ReplyDeleteVisit us: Java Online Training
Visit us: Core java training
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 .
ReplyDeleteThanks 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.
ReplyDeleteBest MPSC coaching Centre in Borivali
Top MPSC coaching Center in Borivali
Im obliged for the blog article.Thanks Again. Awesome.
ReplyDeletecore java online Training
java online training hyderabad