Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java packages

A package is a group of related classes and interfaces that work together to provide a wide variety of functionality for various purposes.

This tutorial focuses on:

Different Java packages

Java provides many different packages:

Importing packages

To be able to use classes and interfaces located in a package, you have to import the package they are in. If you try to use a class or interface without importing the package it is located in, you will get an error. Packages are imported using the import keyword.

Three import methods:

Import an entire package - This means you will be able to use all the classes and interfaces in that package. To do this use the package name followed by the * character.

//imports the entire java.awt package import java.awt.*;

Import individual classes - You will be able to use just those classes.

//imports the BufferedReader class from java.io package import java.io.BufferedReader;

Import individual interfaces - You will be able to use just those interfaces.

//imports the ActionListener interface from java.awt.event package import java.awt.event.ActionListener;
Importing packages in an actual program:
import java.util.*; import java.io.*; import java.awt.event.ActionListener; class ProgramWithPackages{ public static void main(String[] args){ System.out.println("This program imports some packages"); } }

NOTE: The import statement should be the first thing in your code, even before the class declaration!

© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information