Custom package in java

Previous
Next

Custom packages:

  • Java library is a collection of packages.
  • A programmer can create custom package depends on application requirement.
  • ‘package’ keyword is used to create the package.
  • ‘package’ statement must be the first statement in the source file.

Structure:

  1. Package statements
  2. Import statements
  3. Classes

Syntax:

      package <Identity> ;

Example:

      package nit ;

Program code : Save the file on desktop

package nit;
import java.lang.*;
class First 
{
	public static void main(String[] args) 
	{
		System.out.println("nit.First class main...");
	}
}

Compile: We compile the above code using ‘–d’ command and by specifying the package creation location.

Syntax:

            cmd:\> javac  -d  path  sourcefile

Example:

            cmd:\> javac  -d  …/desktop/  First.java

Run the file belongs to package:

Previous
Next

Add Comment

Courses Enquiry Form