0% found this document useful (0 votes)
18 views17 pages

Packages

The document discusses how to create and import user-defined packages in Java. It explains how to name packages, choose a root directory, add the directory to the classpath, add package statements to source files, and access classes from packages. It also covers built-in Java packages and scopes defined by classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views17 pages

Packages

The document discusses how to create and import user-defined packages in Java. It explains how to name packages, choose a root directory, add the directory to the classpath, add package statements to source files, and access classes from packages. It also covers built-in Java packages and scopes defined by classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Packages

• Package is a group of classes.

1
Creating and importing an user defined package

1. Pick a name for your package

Ex : 1. mypackage
2. mypackage.util

– java recommends lower case letters to the


package names

2
2. Choose a directory in your hard drive as the
root directory of your package.
– You need a place on your hard disk to store your
packages.
– For example, create a directory such as c:\mylib
– This folder becomes the root directory for your
packages.

3
3.Create subdirectories within the root directory
for your package name.

For example, for the package named mypackage.util, create


a directory named mypackage in the c:\mylib. Then, in the
mypackage directory, create a directory named util.

4
4. Add the root directory for your package to
the classpath environment variable.

 Do not disturb any paths already listed in the classpath

– For example, suppose your classpsath is already set to


this:
C:\Program Files\Java\jdk1.5.0_05\lib;

– Then, you modify it to look like this:


C:\Program Files\Java\jdk1.5.0_05\lib;c:\mylib;

5
5. Add a package statement at the beginning of
each source file.

• All classes declared within that file belong to the specified


package.

• For example: package mypackage.util;

• The package statement must be the first statement in the file.

6
6. Save the files for any classes you want to be
in a particular package in the corresponding
package directory.

-- For example, save the files for a class that belongs to

the mypackage.util package in


c:\mylib\mypackage\util

7
Ex:
package mypackage.util;
public class Sum
{
public int sumInt(int a[])
{

int s=0;

for(int i=0;i<a.length;i++)
{
s = s+a[i];
}

return s;
}
}
8
Contd..
import mypackage.util.*;
class PackageDemo
{
public static void main( String args[])
{
int x[] = {1,2,3,4,5};
Sum s = new Sum();
System.out.println(s.sumInt(x));
}
}

Note: This file can be compiled and executed from any


place

9
• In general, a Java source file can contain any (or all)
of the following four internal parts:

– A single package statement (optional).


– Any number of import statements (optional).
– A single public class declaration (required).
– Any number of classes private to the package
(optional).

10
Java Built-In Packages
• There are six built-in packages :

– java.lang
• Contains classes for Strings, Math functions, Threads, and Exception
– java.util
• Contains classes such as Scanner, Vector,date, Calendar etc.
– java.io
• Stream classes for I/O
– java.awt
• Classes for implementing GUI – Windows, Buttons, Menus etc.
– java.net
• Classes for Network programming
– Java.sql
• Classes for Database accessing.
– java.applet
• Classes for creating and implementing applets

11
Accessing Classes from Packages
• There are two ways of accessing the classes
from packages:

1. Using fully qualified class name


• java.lang.Math.sqrt(x);

2. Import package and use class name directly


• import java.lang.Math;
• Math.sqrt(x);

12
• Selected class or all classes in packages can be
imported:

– import package.ClassName;
– import package.*;

13
Scopes defined by a class
There are five categories of visibility for class members.

– Same class
– Subclasses in the same package.
– Non-subclasses in the same package.
– Subclasses in different packages.
– Non-subclasses in different packages.

14
To summarize

Class member access

15
Contd..
• We can simplify access protection as follows:
– Anything declared public can be accessed from
anywhere.
– Anything declared private can be accessed within the
same class only.
– default is visible to the same class, subclasses and non-
subclasses in the same package.
– protected is visible to the same class, subclasses, non-
subclasses in the same package and subclasses in different
packages.

16
Class / Interface access modifiers

• A class / Interface has two access levels


– default
– public
• When a class is declared as public, it is accessible by any
other code.
• If a class has default access, then it can only be accessed by
other code within the same package.

17

You might also like