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

Java Packages

Uploaded by

Janani Selvaraj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Packages

Uploaded by

Janani Selvaraj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Dr.

SNS Rajalakhsmi College of Arts and Science


(An Autonomous Co-Education Institution)
Coimbatore – 641049
www.drsnsrcas.ac.in

PACKAGES IN JAVA

Presentation by
Mrs.S.Janani
Assistant Professor
Department of Information Technology
[email protected]
Department of

IT
Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 1
INTRODUCTION
• Packages are Java’s way of grouping a number of related classes
and/or interfaces together into a single unit. That means, packages
act as “containers” for classes.
• The benefits of organising classes into packages are:
- The classes contained in the packages of other programs/applications
can be reused.
- In packages classes can be unique compared with classes in other
packages. That two classes in two different packages can have the
same name. If there is a naming clash, then classes can be accessed
with their fully qualified name.
- Classes in packages can be hidden if we don’t want other packages to
access them.
- Packages also provide a way for separating “design” from coding.

Department of
www.snsgroup.com
IT Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 2
Java provides a large number of classes groped into different packages
based on their functionality
The six foundation Java packages are:
- java.lang :
Contains classes for primitive types, strings, math functions,
threads, and exception.
- java.util :
Contains classes such as vectors, hash tables, date etc.
- java.io :
Stream classes for I/O .
- java.awt :
Classes for implementing GUI – windows, buttons, menus etc.
- java.net :
Classes for networking .
- java.applet :
Classes for creating and implementing app.
Department of
www.snsgroup.com
IT
Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 3
Predefined Macros
ANSI C defines a number of macros. Although each one is available for use in
programming, the predefined macros should not be directly modified.

Macro & Description


DATE__The current date as a character literal in "MMM DD YYYY" format.

TIME__The current time as a character literal in "HH:MM:SS" format.

FILE__This contains the current filename as a string literal.

LINE__This contains the current line number as a decimal constant.

STDC_Defined as 1 when the compiler complies with the ANSI standard.

Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No : 4
Example

The packages are organised in a hierarchical structure .For


example, a package named “java” contains the package
“awt”, which in turn contains various classes required for
implementing GUI (graphical user interface).

Department of
www.snsgroup.com
IT Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 5
Accessing Classes from Packages

There are two ways of accessing the classes stored in


packages:
Using fully qualified class name
java.lang.Math.sqrt(x);
Import package and use class name directly
import java.lang.Math
Math.sqrt(x);
Selected or all classes in packages can be imported:

import package.class;
import package.*;

Implicit in all programs: import java.lang.*;


package statement(s) must appear first
Creating Packages

Java supports a keyword called “package” for creating user-defined


packages. The package statement must be the first statement in a Java
source file (except comments and white spaces) followed by one or
more classes.
package myPackage;
public class ClassA {
// class body
}
class ClassB {
}
Package name is “myPackage” and classes are
considered as part of this package; The code is saved in a file called
“ClassA.java” and located in a directory called “myPackage”.

Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No : 7
USING PACKAGE

Let us store the code listing below in a file named“ClassA.java” within subdirectory
named “myPackage”within the current directory (say “abc”).
package myPackage;
public class ClassA {
// class body
public void display()
{
System.out.println("Hello, I am ClassA");
}
}
class ClassB {
// class body
}

Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :8
Compiling and Running

en ClassX.java is compiled, the compiler compiles it and places .class file in


current directly. If .class of ClassA in subdirectory “myPackage” is not found,
it comples ClassA also.

Note: It does not include code of ClassA into ClassX ✞


When the program ClassX is run, java loader looks for ClassA.class file in a
package called “myPackage” and loads it.

Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :9
USING PACKAGE

Let us store the code listing below in a file named“ClassA.java” within


subdirectory named“secondPackage” within the current directory (say“abc”).

public class ClassC {


// class body
public void display()
{
System.out.println("Hello, I am ClassC");
}
}

Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :10
Department of
www.snsgroup.com
IT
Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024

You might also like