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:
- Pre Defined Package
- 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.
Create a subdirectory pack and save the file ‘one.java’.
- package pack;
- public class one {
- public static void temp() {
- System.out.println("Welcome");
- }
- }
Create a subdirectory sub pack in the package directory and save the file ‘two.java’.
- package pack.subpack;
- public class two {
- public static void temp1() {
- System.out.println("Have a Nice Day...");
- }
- }
Save file ‘threejava’ into the main directory.
- import pack.*;
- import pack.subpack.*;
- class three {
- public static void main(String arg[]) {
- one.temp();
- two.temp1();
- }
- }
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.