0% found this document useful (0 votes)
81 views

Packages in Java

Packages in Java allow for organizing classes and avoiding naming collisions. Packages provide a mechanism for partitioning the class namespace into more manageable chunks. To define a package, use the package keyword at the beginning of a .java file and store the file in a subdirectory matching the package name. Classes within a package must be accessed using import statements. The Java API includes built-in packages like java.lang for core classes and java.util for utility classes.

Uploaded by

Nandana Boban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Packages in Java

Packages in Java allow for organizing classes and avoiding naming collisions. Packages provide a mechanism for partitioning the class namespace into more manageable chunks. To define a package, use the package keyword at the beginning of a .java file and store the file in a subdirectory matching the package name. Classes within a package must be accessed using import statements. The Java API includes built-in packages like java.lang for core classes and java.util for utility classes.

Uploaded by

Nandana Boban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

⮚ Packages in java

● A package in java is a container for the class that is used to keep the class name space
compartmentalized. Here we organize files into different directories according to their
functionality. Packages are collection of classes and interfaces to facilitate a number of
readymade solutions.
● It allows flexibility to give same name to many classes, that is to avoid name space
collision
● It provides mechanism for partitioning the class name space into more manageable
chunks.
● It supports reusability and maintainability
● Access limitation can be applied with the help of packages.
● Nesting of package is possible.

✔ Packages are two types . Build in packages(java API packages) and user
defined.
Creating a user defined package
1. Use package statement at the beginning of the package file.

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

3. Create subdirectory under the working directory with the same name as the package
name.
4. Store the file with the same name as the className.java in the subdirectory created

5. Store the compiled version file(i.e .class) into the same sub-directory

Defining a package:
package <package name>;
public class <class name>
{…………….
……………..
}

✔ Packages are accessible from the program by using import statement

Eg: import java .lang.*; or import java.lang.string;


Adding a class to a package.
example:
package p1:
public class B
{
// body of B
}
1. Define the class and make it public

2. Place the package statement before the class definition.

3. Store this as B.java file under the directory p1.

4. Compile B.java file. This will create a B.class file and place it in the directory
p1.
Example:
package myPack;
public class Balance
{
String name;
double bal;
public Balance(String n, double b)
{
name=n;
bal=b;
}
public void show()
{
System.out.println(name+","+bal);
}}
Adding more classes to a Package :

We can add more classes to a created package by using package name at the top of the program and
saving it in the package directory. We need a new java file to define a public class, otherwise we can
add the new class to an existing .java file and recompile it.
Accessing classes inside a package

Consider following two statements :mport the Vector class from util package.

• // import the Vector class from util package.


• import java.util.vector;
• // import all the classes from util package
• import java.util.*;
First Statement is used to import Vector class from util package which is contained inside java.Second
statement imports all the classes from util package.

Example:

// Name of the package must be same as the directory

// under which this file is saved

package myPackage;

public class MyClass

public void getNames(String s)

System.out.println(s);

}}

Now we can use the MyClass class in our program.

/* import 'MyClass' class from 'names' myPackage */

import myPackage.MyClass;

public class PrintName

public static void main(String args[])

// Initializing the String variable

// with a value

String name = "GeeksforGeeks";


// Creating an instance of class MyClass in

// the package.

MyClass obj = new MyClass();

obj.getNames(name);

Note : MyClass.java must be saved inside the myPackage directory since it is a part of the package.

Java API packages


Built-inPackages
These packages consist of a large number of classes which are a part of Java API. Some of the
commonly used built-in packages are:

1) java.lang: Contains language support classes(e.g classed which defines primitive


data types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user interfaces
(like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.

You might also like