Package in java

Package in java

  • A java package is a collection of classes or interfaces.

Package in java can be categorized in two form

  1. built-in package
  2. .user-defined package.


There are many built-in packages

  • java.lang
  • java.awt
  • java.javax
  • java.swing
  • java.util
  • java.sql etc.


 Advantage of Java Package

  • Java package is used to categorize the classes and interfaces so that they can be easily maintained.
  • Java package provides access protection.
  • Java package removes naming collision.


How to access package from another package?

There are three ways to access the package from outside the package.
1.import package.*;
2.import package.classname;
3.fully qualified name.

Example of packages
package com.spy.coder   // package declaration
import basic.Method     // importing package

public class Sample 
	public static void main(String[] args)
	         System.out.println("this is exaple of package");
		 Method a=new Method();
		 System.out.println("k value: "+a.k);

Leave a Reply