Page Stats
Visitor: 257
Java Packages
A package is a collection of related classes and interfaces providing access protection and namespace management.
Types of Packages
- Pre Defined packages
- User Defined packages
Pre Defined packages - Java Libraries
- java.lang: lang stands for language. This package includes classes for primitive data types, mathematical functions, threads, exceptions etc.
- java.util: util stands for Utility. It contain classes such as vectors, hash tables, random numbers, Date and time functions.
- java.io: It is a collection of streams and random access files. It provide a wide variety of input and output functions, like read, readLine().
- java.awt: awt stands for abstract windowing toolkit. This library contains classes for basic interface components such as windows, events, colors, font and controls such as buttons lists, menu, scroll bars and so on.
- java.net: net stands for networking. This package provides classes for communicating with local computers as well as Internet servers. The system requires TCP/IP connection to work with java.net.
- java.applet: This package contains classes for creating and implementing applets. Applets need Internet browser to display outputs.
User Defined packages
Creating of a package it involves the following steps:
- Declare the package at the beginning of the file. E.g. package <package name>;
- Define the class that it to be put in the package & declare it public.
- Create a sub-directory under the directory where the main source files are stored.
- Store the listing as the class name. java name in the sub-directory
- Compile the file javac
. This will create a file .class in the file directory.
Example : simple package
package package1; public class classA { public void display_A() { System.out.println("Close A"); } }
import package1.classA; class package_test1 { public static void main(String argc[]) { classA objectA = new classA(); objectA.displayA(); } }