Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
71 views

Java Packages

A package is a collection of classes, interfaces, and subpackages that are used to organize code and separate an application's code from library code. Packages allow classes to be reused, contain hidden classes, and have multiple classes with the same name. To create a package, the package name is declared at the top of a file and the class is defined as public. The file is saved in a subdirectory matching the package name and compiled. Classes can access other classes in their package or public classes from other packages by importing the package.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Java Packages

A package is a collection of classes, interfaces, and subpackages that are used to organize code and separate an application's code from library code. Packages allow classes to be reused, contain hidden classes, and have multiple classes with the same name. To create a package, the package name is declared at the top of a file and the class is defined as public. The file is saved in a subdirectory matching the package name and compiled. Classes can access other classes in their package or public classes from other packages by importing the package.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Packages

A package is a collection of classes, java interfaces and subpackages.

Packages are convenient for organizing our work and for separating our work
from code libraries provided by others. The standard Java library is distributed
over two main packages ‘java’ and ‘javax’.

Benefits of using packages:

1) The classes contained in the packages of other programs can be easily


reused.

2) Packages can contain hidden classes not visible outside the packages.

3) Different packages can have the classes with same name.

Package Contents
name
java.lang It contains the language support classes. These are classes
that java compiler itself uses and therefore they are
automatically imported.

java.util It contains language utility classes, such as vectors, hash


tables, random numbers, date, etc.
java.io It contains input/output support classes which facilitate for
the input and output of data.
java.awt It contains the classes for implementing a GUI. They include
classes for windows, buttons, lists, menus and so on.
java.net It contains the classes for networking which facilitate
communication with local computers as well as with internet
servers.
java.applet It contains the classes for creating and implementing the
applets ( small java codes executed in a web browser )
Creating a Package
A package can be created with the following steps:

1) Declare the package at the beginning of a file using the keyword package
followed by a package name.

package mypackage;

2) Define the class that is to be put in the package and declare it public.

package mypackage;

public class FirstClass


{
………
………
}

3) This source file has to be saved as a file with name FirstClass.java and
located in a subdirectory named ‘mypackage’ under the directory where the
working source files are stored.

4) Finally, the file has to be compiled to create FirstClass.class file in the


subdirectory that has the same name as the package. Now the new package
‘mypackage’ is ready to use.

Accessing a Package
A class can use all classes from its own package and all public classes from
other packages.

The public classes in another package can be accessed in two ways. The first is
simply to add the full package name in front of every class name.

For example:

Java.util.Scanner scan = new java.util.Scanner();

The second way is to use import statement. The point of the import statement
is simply to give shorthand to refer to the classes in the package. Once the
import is used, the classes have no longer been given their full names. Specific
classes or the whole package can be imported using import keyword. The
import statement should be placed at the top of the source file but below any
package statements.

For example:

import java.util.*; // importing the whole ‘util’ package

Scanner scan = new Scanner ();

Or

import java.util.Scanner; //importing a specific class

Scanner scan = new Scanner ();

An Example
First.java (should be saved in mypackage subdirectory)

package mypackage;

public class First


{
public void display()
{

System.out.println (“I am from \’First\’ class”);

System.out.println (“which is in \’mypackage\’. “);

}
}

PackageTest.java (should be stored in main working directory)

import mypackage.First;

public class PackageTest{

public static void main (String args[]){

First f = new First ();

f.display();

}
}

Adding a Class to a Package


To add a class to a package, the name of the package is to be put at the top of
the source file, before the code that defines the class to be added. The detailed
procedure is described by the following steps:

1) Define the class and make it public.

2) Place the package statement before the class definition as follows:

package mypackage;

public class SecondClass {

--------
--------

3) Store this file as SecondClass.java under the directory ‘mypackage’.

4) Compile SecondClass.java file to create a SecondClass.class file in the


directory mypackage.

Now both FirstClass and SecondClass are the members of ‘mypackage’


package. The directory structure looks like:

Hiding Classes
When a package is imported using asterisk (*), all public classes are imported.
However, the classes that are not declared public are not imported. This allows
hiding these classes from accessing from outside of the package.

Example:

package package1;

public class First


{ /
/body of First }

class Second
{
//body of Second
}

-----

import package1.*;

First f = new First (); // OK; class First can be accessed

Second s = new Second (); // ERROR; class Second cannot accessed

Static Imports
Starting with Java SE 5.0, the import statement has been enhanced to permit
the importing of static methods and fields, not just classes.

For example, if the directive

import static java.lang.System.*;

is added to the top of the source file, then static methods and fields of the
System class can be used without the class name prefix:

out.println(“Adios, Amigos!”); // that is System.out

exit(0); //that is System.exit

If a static import is used for Math class, the math functions can be used in a
more natural way. For example,

sqrt(pow(x,2) + pow(y,2))

Seems much clearer than


Math.sqrt(Math.pow(x,2) + Math.pow(y,2)).

You might also like