Program to create user-defined package named "p1" and put class "A" in it
A package can be defined as a group of similar types of classes, interface, enumeration and sub-package. Package are used in Java, in-order to avoid name conflicts and to control access of class, interface and enumeration etc. Using package it becomes easier to locate the related classes.
Package in java can be categorized in two form,
- Built-in package : Existing Java package for example java.lang, java.util, java.awt, etc
- User-defined package : Java package created by user to categorized classes and interfaces. .
Creating a package in java is quite easy. Simply include a package command followed by name of the package as the first statement in java source file.
package mypackage;
public class MyClass
{
...statements;
}
The above statement create a package called mypackage. Java uses file system directory to store package. For example the .class for any classes you define to be part of mypackage package must be stored in a directory called mypackage.
PROGRAM
PROGRAM
package p1;
public class A
{
public void dispA()
{
System.out.println("Inside from class A");
}
}
HOW TO COMPILE ?
To compile the above package program use the following command.
javac -d . A.java
After successful compilation the mypackage directory will be created and the .class file is placed in that directory. The output of this program is the .class file.