Package: Java - Lang Java - Awt Java - Applet Javax - Swing Java - SQL
Package: Java - Lang Java - Awt Java - Applet Javax - Swing Java - SQL
Any classes declared within that file will belong to the specified package. The package statement
defines a name space in which classes are stored. If you omit the package statement, the class
names are put into the default package, which has no name and is called un-named package.
Syntax:
package pakage_name;
Eg:
package MyPack;
Java uses file system directories to store packages. Remember that it is case significant and
directory name must match the package name exactly. More than one file can include the same
package statement.
You can create a hierarchy of packages. To do so, simply separate each package name from the
one above it by use of a period or dot operator.
Eg
package p1;
import java.util.*;
int id;
String name;
id=scan.nextInt();
name=scan.next();
System.out.println(id+"\t"+name);
}
Compile java package
If you are not using any IDE, you need to follow the syntax given below:
javac -d . javafilename
Eg:
Javac –d . Person.java
Eg 1:
import p1.Person;
class Student
p.set_data();
p.display();
}
Eg2.
class Student
p.set_data();
p.display();
Package inside the another package is called the sub-package. It should be created to categorize
the package further.
package mypack.p1;
import java.util.*;
int id;
String name;
id=scan.nextInt();
name=scan.next();
System.out.println(id+"\t"+name);
Importing Package
import mypack.p1.Person;
class Student
p.set_data();
p.display();
}
// Demonstrate the use of package in inheritance
Q: created a class Calculator with the following function i.e add(), subs(), multi() and div() inside a
package name letmecalculate. Create a menu driven program in Test class and use the class
Calculator in the Test class.