Easy Tutorial
For Competitive Exams

Java Programming Packages

What is Java Package?

A package is a collection of related classes and interfaces,etc.,

Each package in Java has its unique name and organizes your classes and interfaces into a folder structure and make it easy to locate and use them.

More importantly, it helps improve re-usability.

Types of Package:

  • Built-in Package
  • User-defined-package

What is Built-in Package?

These packages consists of a large number of classes which are a part of Java API.

For e.g, we have used java.io package previously which contain classes to support input / output operations in Java.

Similarly, there are other packages which provides different functionality.

Package Name
Description
java.lang Contains language support classes ( for e.g classes which defines primitive data types, math operations, threads,exceptions,etc.) . This package is automatically imported.
java.io Contains classes for supporting input / output operations.
java.util Contains utility classes which implement data structures like Linked List, Hash Table, Dictionary, etc and support for Date / Time operations.
java.applet Contains classes for creating Applets.
java.awt Contains classes for implementing the components of graphical user interface ( like buttons, menus,list, etc. ).
java.net Contains classes for supporting networking operations.They include classes for communicating with local computers as well as with internet servers.

What is User-defined-package?

User-defined-package is defined by programmers to bundle group of related classes,interfaces,etc.,

How to Creat a package?

Creating a package in java is quite easy. Simply include a package command followed by name of the package as the first statement in java source file.

Syntax

package packagename;
Example:package mypack;

The above statement create a package called mypack.

Example

package mypack;
public class A{
public void show(){
System.out.println("Show method");
  }
}

What is import keyword?

import keyword is used to import built-in and user-defined packages into your java source file.

So that your class can refer to a class that is in another package by directly using its name.

There are 2 different ways to refer to class that is present in different package.

How to import the only class you want to use?

Example

package Demo;
import java.util.Date;
class MyDate extends Date{
 //statements
}

How to import all the classes from the particular package?

Example

package Demo;
import java.util.*; 
class MyDate extends Date{
//statement;
}

Where the import statement is kept in java?

import statement is kept after the package statement.

Example

package Demo;
import java.util.*;

But if you are not creating any package then import statement will be the first statement of your java source file.

Example

package mypack;
public class A{
public void show(){
System.out.println("Show method");
   }
}

package demo;
import mypack.A;
public class Hello{
public static void main(String args[]){
A a=new A();
a.show();
System.out.println("show method from A class");
  }
}

Solve this!!

11695.Which of the following is correct way of importing an entire package ‘pkg’?
import pkg.
Import pkg.
import pkg.*;
Import pkg.*;
11696.Which of these access specifiers can be used for a class so that it’s members can be accessed by a different class in the different package?
public
protected
private
No Modifier
Share with Friends