What is a package and how to work with package in java

package is a group of related classes and interfaces. A group of packages are called a library.Every package have its unique name.

Packages are two types :

package in java Built-in packages:
package in java These packages (Java API) are already developed by the java people.Programmer can use the Built-in packages by importing it.
package in java Ex:java.util , , java.lang , ..


The outer box represents the main package (java) and the inner boxes are sub packages.


package in java User-defined packages:
package in java Programmers also can create their own packages.These packages are called user-defined packages.To use these packages , import it by it’s name.
package in java Ex:pack1.DateUtil ,pack2.utility.


Example for package :

Type the below command (on command prompt)  to Compile  .

C:\> javac -d .
package in java  The -d tells to the java compiler ,to create a separate directory (package1) and place the .class file (mathOperations.class) in it.

package in java  The “.”  indicates that the package should be created in the current directory i.e C:\.

Compile the program with the command.
C:\> javac -d .
package in java  Once compiled, the java compiler creates a sub directory with the name pack in package1 and stores the dateOperations.class file inside it.

package in java  The absolute path is  C:\package1\pack\dateOperations.class .

package in java  The main package cannot load the classes of a sub package. so add the sub package name to the import statement like  import package.subpackage.* ; 
package in java  Use * to import all  classes from the  package.

Related Posts :
   [will not be published]

^ <