Package

A package is a group of related classes, interfaces, enums and subclasses.
It is a logical container that contains logically related classes, interfaces and sub packages. Concept of package is used to provide a unique name space to its members as well as to enforce scopes, rules.
Package is a name i.e. associated to the class name so that classes can be uniquely identified.

Creating package
package keyword is used to create package. It must be the first statement in the source file.

Syntax :

package <fully qualified package name> ;

Example :

package employee; public class EmployeeInfo { int empId; String name; public void getDetail() { System.out.println("Employee getDetail"); } }

How to Compile & Run package ?
There are different way to compile & run a package class:
To Compile:-

Crate folder in the same directory.

C:\JavaProgram\package>javac -d . EmployeeInfo.java

To Run :

C:\JavaProgram\package>java employee.EmployeeInfo
To Compile:-
Create folder in the given directory
C:\JavaProgram\package>javac -d d:\MyJava EmployeeInfo.java

To Run:-

First we need to set classpath and after that run the program.
C:\JavaProgram\package>set classpath=d:\MyJava
C:\JavaProgram\package>java employee.EmployeeInfo
Another way to set classpath and run the programm simultaneously.
C:\JavaProgram\package>java -classpath d:\MyJava employee.EmployeeInfo

How to access package from another package?
There are three ways to access the package from outside the package.
  1. import package_name.*;
  2. import package_name.classname;
  3. fully qualified package_name.
The import keyword is used to access certain classes, or entire packages, into visibility.
In Java source code, import statements write immediately the package statement (if it exists) and before any class definition.
If we use import packagename.* then all the classes and interfaces of this package will be accessible but not subpackages.
Example of import package_name.*;
package employee; public class EmployeeInfo { int empId; String name; public EmployeeInfo(int empId,String name) { this.empId=empId; this.name=name; } public void getDetail() { System.out.println("Employee getDetail"); System.out.println("Employee Id : "+empId); System.out.println("Employee Name : "+name); } }

Save as : EmployeeInfo.java
package report; import employee.*; public class EmployeeReport { int empId; String name; public static void main(String s[]) { EmployeeReport empReport=new EmployeeReport(); EmployeeInfo empInfo=new EmployeeInfo(123,"Anand"); empInfo.getDetail(); } }

Save as : EmployeeReport.java

Output

C:\JavaProgram\package>javac -d . EmployeeReport.java

C:\JavaProgram\package>java report.EmployeeReport
Employee getDetail
Employee Id : 123
Employee Name : Anand

Download this example
Example of  import package_name.classname;
package employee; public class EmployeeInfo { int empId; String name; public EmployeeInfo(int empId,String name) { this.empId=empId; this.name=name; } public void getDetail() { System.out.println("Employee getDetail"); System.out.println("Employee Id : "+empId); System.out.println("Employee Name : "+name); } }

Save as : EmployeeInfo.java
package report; import employee.EmployeeInfo; public class EmployeeReport { int empId; String name; public static void main(String s[]) { EmployeeReport empReport=new EmployeeReport(); EmployeeInfo empInfo=new EmployeeInfo(123,"Anand"); empInfo.getDetail(); } }

Save as : EmployeeReport.java

Output

C:\JavaProgram\package>javac -d . EmployeeReport.java

C:\JavaProgram\package>java report.EmployeeReport
Employee getDetail
Employee Id : 123
Employee Name : Anand

Download this example
Example of fully qualified package_name;
package employee; public class EmployeeInfo { int empId; String name; public EmployeeInfo(int empId,String name) { this.empId=empId; this.name=name; } public void getDetail() { System.out.println("Employee getDetail"); System.out.println("Employee Id : "+empId); System.out.println("Employee Name : "+name); } }

Save as : EmployeeInfo.java
package report; public class EmployeeReport { int empId; String name; public static void main(String s[]) { EmployeeReport empReport=new EmployeeReport(); employee.EmployeeInfo empInfo=new employee.EmployeeInfo(123,"Anand"); empInfo.getDetail(); } }

Save as : EmployeeReport.java

Output

C:\JavaProgram\package>javac -d . EmployeeReport.java

C:\JavaProgram\package>java report.EmployeeReport
Employee getDetail
Employee Id : 123
Employee Name : Anand

Download this example
 



 

Latest Tutorial


Most Visited Tutorial