Java - Package

Introduction

In this blog, I will explain about the Package concept, using 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 sub directory pack and save the file ‘one.java’.

  1. package pack;  
  2.   
  3. public class one  
  4.  {  
  5.    public static void temp()  
  6.     {  
  7.        System.out.println("Welcome");  
  8.     }  
  9.  } 

Create a sub directory subpack in the package directory and save the file ‘two.java’.

  1. package pack.subpack;  
  2.   
  3. public class two  
  4.  {  
  5.    public static void temp1()  
  6.     {  
  7.        System.out.println("Have a Nice Day...");  
  8.     }  
  9.  } 
Save file ‘threejava’ into the main directory.
  1. import pack.*;  
  2. import pack.subpack.*;  
  3.   
  4. class three  
  5.  {  
  6.    public static void main(String arg[])  
  7.     {  
  8.        one.temp();  
  9.        two.temp1();  
  10.     }  
  11.  } 
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 subpack directory.

 

Compile and execute three.java in the main directory.

 
Ebook Download
View all
Learn
View all