bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Java Packages

 

Package is a collection of similar types of classes, interfaces and sub packages. It is used:

  • To prevent naming conflicts
  • To control access
  • To categorize the classes and interface so they are easily maintained.

But if you import a package subpackages will not be imported.

 

Package in java are of two types:

  • Built-in Package
  • User-defined Package

 

1. Built-in Package – The packages which are already created that is pre defined is known as built in packages. The most common built-in package in java are:

built in package

java.lang: Collection of fundamental classes. This package is automatically imported.

java.util: Contains utility classes which implement data structures like Hash Table, Linked List etc and support for Date & Time operations.

java.io: Contains classes for supporting input & output operations

java.net: Contains classes for supporting networking operations

java.awt: Contains classes for implementing the components of graphical user interface like buttons, menus, etc.

java.applet: Contains classes for creating applets

 

2. User Defined Packages – Packages which are created by user is known as user defined package.

 

To create package use keyword package with package name as follows:

package package_name;

To use the package in the class use import keyword. There are 3 ways to import package:

Suppose there is a package Intellipaat. Save it as A.java

package Intellipaat;

public class A

{

public void print(){

System.out.println("Hello Intellipaat");

}

}

 

1. import package.*; – If you want to implement all classes and interface of that package.

e.g.

//save by B.java

import Intellipaat.*;  // It will access all Class and interface of package Intellipaat

class B{

public static void main(String args[]){

A a = new A();

a.print();

}

}

Output

Hello Intellipaat

 

2. import package.classname; – It implements only specified class.

e.g.

//save by B.java

import Intellipaat.A;  // It will access only Class A of package Intellipaat

class B{

public static void main(String args[]){

A a = new A();

a.print();

}

}

 Output

Hello Intellipaat

 

3. Fully qualified name – In this there is no need to import the package but you require to use fully qualified name each time when you are using class or interface. When you use fully classified name then only declared class of this package will be accessible.

e.g.

//Save as B.java

class B{

public static void main(String args[]){

Intellipaat.A a = new Intellipaat.A();//using fully qualified name

a.print();

}

}

Output

Hello Intellipaat

Learn more about Cross-Platform Mobile Technology for Android and iOS using Java in this insightful blog now!

"0 Responses on Java Packages"

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 05th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Java Packages.