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.

built-in package in java

  • java.lang
  • java.awt
  • java.javax
  • java.swing
  • java.net
  • java.io
  • 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.

Package declaration in java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
package com.spy.coder   // package declaration
import basic.Method     // importing package

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

Leave a Reply

Your email address will not be published. Required fields are marked *