0% found this document useful (0 votes)
2 views5 pages

Java Package

The document explains Java packages, which are collections of classes and interfaces organized into namespaces. It details built-in API packages like java.util and java.awt, as well as user-defined packages, and describes how to create and access these packages using import statements. Additionally, it covers naming conventions, the visibility of classes, and the use of static imports for accessing static members directly.

Uploaded by

punith372006
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)
2 views5 pages

Java Package

The document explains Java packages, which are collections of classes and interfaces organized into namespaces. It details built-in API packages like java.util and java.awt, as well as user-defined packages, and describes how to create and access these packages using import statements. Additionally, it covers naming conventions, the visibility of classes, and the use of static imports for accessing static members directly.

Uploaded by

punith372006
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

 java.

util: Contains utility classes that implement data structures


Packages such as Linked Lists and Dictionaries, as well as support for date
A package in Java is a similar type of classes, interfaces, and sub- and time operations.
packages into a single unit  [Link]: Contains classes for creating Applets.
Types of packages  [Link]: Contains classes for implementing the components for
[Link] packages graphical user interfaces (like buttons, menus, etc).

[Link] defined packages

[Link] defined packages


The packages are organized in a structure. This shows that the package
name java contains the package AWT (Abstract Window Toolkit), which
in turn contains various classes required for implementing graphical
user interface.

Benefits of packages

 The class contained in the packages of other programs can be


easily reused

 Packages in Java provide a way to organize your classes and


interfaces into a single namespace.

[Link] packages

Built-in Packages
There are two process of accessing the classes stored in a package.
Built-in Packages comprise a large number of classes that are part of
the Java API. Some of the commonly used built-in packages are:  Using packagename."

 Using fully qualified class name


 [Link]: Contains language support classes(e.g, classes that Using packagename.*
define primitive data types, math operations). This package is
If you use package.* then all the classes and interfaces of this
automatically imported.
packagewill be accessible but not the subpackages
 [Link]: Contains classes for supporting input/output operations.
The import keyword is used to make the classes and interface of
another package accessible to the current package.  Store the listing as the [Link] file in the subdirectory
created.
Syntax

import packagename.*;
 Compile the file. This creates class file in the subdirectory.

Example
Syntax:
import [Link].*;
package packagename;
Will bring all classes of [Link] package.
public class classname
{
Naming Conventions // data members
// member functions
}
Packages can be named using the standard Java naming rules. By
convention, however: Packages always begin with lower case letters. Example:
The entire class name begins with upper case letters.
package department;
Example: public class CS
{
double z = [Link] (x, 2); public void display()
{
[Link]("Department of Computer Science");
}
Class name }

Package name Here the package name is department. The class CS is now considered a part of this
package. The source file should be named [Link] and stored in the subdirectory
method name department.
The resultant [Link] will be stored in the same subdirectory.
Creating Packages
Java supports functionality that users can define their own packages.
Accessing a Package
Creating our own packages involves the fallowing steps
The import keyword is used to import built-in and user-defined
packages into your java source file. So that your class can refer
Declare the package at the beginning of a file using the form to a class that is in another package by directly using its name.

package packagename;
There are 3 different ways to refer to class that is present in
 Define the class that is to be put in the package and declare it different package
public.
Example:
 Create a subdirectory under the directory where the main source
files are stored
class NewDate extends [Link]
{ [Link](" Government First Grade College");
//statement; }
}

The source file should be named [Link] and stored in the


subdirectory The resultant [Link] will be stored in the
import the only class you want to use same subdirectory.
Example:
The following code shows a creating package named
import [Link]; department containing class CS.
class NewDate extends Date
{ package department;
// statements public class CS
} {
public void display()
import all the classes from the particular package {
Example: [Link]("Department of Computer Science");
}
import [Link].*; }
class NewDate extends Date
{ The source file should be named [Link] and stored in the
//statement; subdirectory departme resultant [Link] will be stored in the
} same subdirectory.

The following code shows the importing classes from other


Using a Package
packages.

The following code shows a creating package named import [Link];


government containing a single class College. import department.*;
class PackageTest
package government; {
public class College public static void main (String arg[])
{ {
public void display() College ob1=new College();
{ CS ob2-new CS();
[Link]():
[Link]();
}
}
This program may be saved as [Link], compiled and
run to obtain the results Hiding classes
When we import a package within a program, only the classes
Output: declared as public in that package will be made accessible
within this program. In other words, the classes not declared as
Government First Grade College public in that package will not be accessible within this
Department of Computer Science program.

package department;
public class CS // public class, available
outside
Adding a class to packages {
It is simple to add a class an existing package. public void displaycs()
Consider the following package {
[Link]("Department of Computer Science");
package department; }
public class CS }
{ class Maths //not public, hidden
public void displaycs() {
{ public void displaymaths()
[Link]("Department of Computer Science"); {
} [Link]("Department of Mathematics");
} }
}
The package department contains one public class by name CS.
Suppose we want to add another class Maths to this package. Here, the class Maths which is not declared public is hidden
This can be done as follows, from outside of the package department. This class can be
seen and used only by other classes in the same package. Note,
[Link] the class and make it public that a Java source file should contain only one public class and
[Link] the package statement may include any number of non-public classes.
Now, consider the following code, which imports the package
department that contains classes CS and Maths. import static [Link]-type-name.*;

import department.*; Example:


…………….. import static [Link].*; //importing all static member of
…………….. Math class
CS ob1=new CS: //Correct, class CS is available
here
Maths ob2=new Maths(); // Wrong, class Maths is not
available
Example [without using static import]
Static import
The static import is a feature that expands the capabilities of public class Test
import keyword. Its used to import static member class. We all {
know that static member are referred in association with its public static void main(String[] args)
class name outside the class. Using the static import, it is {
possible to refer to the static member directly without its class [Link]([Link](144));
name. }
}
 The first form of static import statement, import only a
single static member of a class Output:-
12.0
Syntax:

import static [Link]-member-name; Import static [Link].*;


public class Test
Example: {
public static void main(String[] args)
import static [Link]; //importing static method {
sqrt of Math class [Link]([Link](144));
}
 The second form of static import statement, imports all }
the static member of a class
Output:-
Syntax: 12.0

You might also like