Java packages | Packages in Java | package java | import package java
“A package is a set of classes that are stored in a directory, which has the same name as the package name. “ Or we can say that Java package is a container, which contain the class file, interfaces etc.Package enable the way to organize file into different directory according to their functionality and usability. The java package are categorized into following two categories: -
- Java API (application programming interface) Packages / pre-define packages
- Java User Define Packages
Java API packages (pre-define): -
The packages that are pre-define in Java language is called java API packages (pre-define packages). The java API consist of various package such as java.utill, java.io, java.lang, java.awt, java.applet etcetera.
Fig: -Hierarchy of the Java Package (pre-define packages)
User Define Java Package: -
The packages that a user creates are called user define packages. These user define packages can be imported in any Java program.Creating a user define Java package: -We can create a user define java package by using the keyword, package. The package declaration must be at the beginning of the source file. Note that we can make only one package declaration in a source file. We can create a user define package by performing following steps: -
- First we create a source file which contain the package definition.
- Create a folder having the same name as package name and save the source file within the folder.
- Compile the source file.
Example of Java Package: -
To Compile java package: - javac –d c:java MyPack.java
-d option:When we compile the program using –d option, the compiler creates the package directory and move the compiled class file to it.
To Compile: javac UsePack.java
To Run: java UsePack
No comments:
Post a Comment