Java - Package

Introduction

 
In this blog, I will explain about the Package concept, using the Java program. It is very simple in Java Programming. The output will be displayed in the Run module.

 
Software Requirement

 
JDK1.3.
 
Package: 
Package is the collection of classes, methods, and interfaces. It is divided into two types, namely:
  1. Pre Defined Package
  2. User-Defined Package

Pre Defined Package

 
Pre Defined Packages are already some of the existing packages.
 
Examples are,
  • java.io.*;
  • java.net.*;
  • java.lang.*;
  • java.awt.*; and etc., 

User-Defined Package

 
User-Defined Packages are the programs, which can define their own packages to bundle the group of classes/interfaces, etc. It is a good practice to group the related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations, annotations are related.
 
Simple Program
 
Create a subdirectory pack and save the file ‘one.java’.
  1. package pack;  
  2. public class one {  
  3.  public static void temp() {  
  4.   System.out.println("Welcome");  
  5.  }  
  6. }   
Create a subdirectory sub pack in the package directory and save the file ‘two.java’.
  1. package pack.subpack;  
  2. public class two {  
  3.  public static void temp1() {  
  4.   System.out.println("Have a Nice Day...");  
  5.  }  
  6. }   
Save file ‘threejava’ into the main directory.
  1. import pack.*;  
  2. import pack.subpack.*;  
  3. class three {  
  4.  public static void main(String arg[]) {  
  5.   one.temp();  
  6.   two.temp1();  
  7.  }  
  8. }  
Explanation
 
In this blog, I will explain about Package concept, using Java Program. It is very simple in Java Programming. The output will be displayed in the Run module.
 
Output
 
Compile one.java in the pack directory. 
 
Compile two.java in the sub pack directory.
 
Compile and execute three.java in the main directory.
Next Recommended Reading User Defined Package in Java