Sie sind auf Seite 1von 4

What Is a Package?

A package is a namespace that organizes a set of related classes and interfaces.


Conceptually you can think of packages as being similar to different folders on your
computer. You might keep HTML pages in one folder, images in another, and scripts
or applications in yet another. Because software written in the Java programming
language can be composed of hundreds or thousands of individual classes, it makes
sense to keep things organized by placing related classes and interfaces into packages.

The Java platform provides an enormous class library (a set of packages) suitable for
use in your own applications. This library is known as the "Application Programming
Interface", or "API" for short. Its packages represent the tasks most commonly
associated with general-purpose programming
You can access public classes in another (named) package using:
package-name.class-name
You can access the public fields and methods of such classes using:
package-name.class-name.field-or-metho
You can avoid having to include the package-name using:
import package-name.*;

Java code is organized into packages. This organization is helpful when you write some Java code that
you want to use in another program. For example, if I wanted to write a class called Foo that I wanted to
use in another program, I would place Foo into a package by using the "package" keyword at the top of
the Foo.java file:

package org.mycode;

Now if I wanted to use my Foo class, I would use an import statement to tell the Java compiler the name
of the package (where it can find Foo). Example:

import org.mycode.Foo;

Now I can use the Foo object like so:

Foo f = new Foo();

You can still use Foo even if you don't use an import statement. You would just need to re-write the code
to:

org.mycode.Foo f = new org.mycode.Foo();

Obviously you have to type much less when using an import statement, so most programmers will use
them.
A package is a collection of related classes.It helps Organize your classes into a folder
structure and make it easy to locate and use them.
More importantly,It helps improve re-usability.
Syntax:-
1 package;
The following video takes you through the steps of creating a package.

Please be patient . Video will load in some time. If you still face issue viewing video click here
Assignment 1:To create a package
Step 1) Copy the following code into an editor
1
2 package p1;
class c1{
3 public void m1(){
4 System.out.println("Method m1 of Class c1");
5 }
6 public static void main(String args[]){
c1 obj = new c1();
7
obj.m1();
8 }
9 }
10
Step 2) Save the file as Demo.java. Compile the file as, javac d . Demo.java
Step 3) Run the code as java p1.c1

Assignment 2) To create a sub-package


Step1) Copy the following code into an editor
package p1.p2;
1
class c2{
2
3 public void m2(){
4 System.out.println("Method m2 of Class c2");
5 }
6 public static void main(String args[]){
7 c2 obj = new c2();
obj.m2();
8 }
9 }
10
Step 2) Save the file as Demo2.java. Compile the file as javac d . Demo2.java
Step 3) Run the code as java p1.p2.c2

importing a package
To create an object of a class (bundled in a package), in your code, you have to use its fully
qualified name.
Ex.
1 java.awt.event.actionListner object = new java.awt.event.actionListner();
But , it could become tedious to type in the long dot-separated package path name for every
class you want to use. Instead it is recommended you use the import statement.
Syntax
1 import ;
Once imported , you can use the class without mentioning its fully qualified name.
Ex:
1 import java.awt.event.*; // * signifies all classes in this package
2 import javax.swing.JFrame // here only the JFrame class is imported
3 //Usage
JFrame f = new JFrame; // without fully qualified name.
4
Assignment 3: To import package
Step 1) Copy the code into an editor.
1
2 // Using packages created in earlier assignment
3 package p3;
import p1.*; //imports classes only in package p1 and NOT in the sub-package p2
4 class c3{
5 public void m3(){
6 System.out.println("Method m3 of Class c3");
7 }
8 public static void main(String args[]){
9 c1 obj1 = new c1();
obj1.m1();
10 p1.p2.c2 obj2 = new p1.p2.c2();
11 obj2.m2();
12 }
13 }
14
Step 2) Save the file as Demo2.java . Compile the file using the command javac d .Demo2.java
Step3) Execute the code using the command java p3.c3

Packages points to note:


To avoid naming conflicts packages are given names of the domain name of the
company in reverse Ex : com.guru99. com.microsoft, com.infosys etc.

When a package name is not specified , a class is into the default package (the current
working directory) and the package itself is given no name. Hence you were able to execute
assignments earlier.

While creating a package, care should be taken that the statement for creating package
must be written before any other import statements
1 // not allowed
2 import package p1.*;
3 package p3;