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

Understanding Java Packages and Usage

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

Understanding Java Packages and Usage

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Package

s
Introduction

• Packages act as “containers” for classes.

• A package represents a directory that contain


related group of classes & interface.
Types of Packages

• There are basically only 2 types of


java packages.
• They are as follow :
– Built –in Packages or System Packages
(from Java API)
– User Defined Packages
Java Foundation Packages
• Java provides a large number of classes grouped into different
packages based on their functionality.
• The six foundation Java packages are:
– [Link]
• Contains classes for primitive types, strings, math functions, threads, and
exception
– [Link]
• Contains classes such as vectors, hash tables, date etc.
– [Link]
• Stream classes for I/O
– [Link]
• Classes for implementing GUI – windows, buttons, menus etc.
– [Link]
• Classes for networking
– [Link]
4 • Classes for creating and implementing applets
Creating Packages
• Java supports a keyword called “package” for
creating user-defined packages.

//save as [Link]
package mypack;
public class Simple{
public static void main(String args[])
{ [Link]("Welcome to
package");
}
}
Accessing a Package
• There are three ways to access the
package from outside the package.

– import package.*;

– import [Link];

– fully qualified name.


Using packagename.*

• If you use package.* then all the classes and


interfaces of this package will be accessible
but not subpackages.

• The keyword is used to


import and makethe interface
classes of
package
another
accessible to the current package.
//save by [Link]
package pack;
public class A{
public void
msg()
{[Link]
ntln("Hello");}
//save by [Link]
}
package mypack;
import pack.*;

class B{
public static void main(String args[]){
A obj = new A();
[Link](); Output: Hello
}
}
Using [Link]
//save by [Link]

package pack;
public class
A{
public void
msg()
{[Link].
//save by [Link]
println("Hell
package mypack;
o");}
import pack.A;
}
class B{
public static void main(String args[]){
A obj = new A(); Output: Hello
[Link]();
}
}
Using fully qualified name

• If you use fully qualified name then only


declared class of this package will be
accessible.
• Now there is no need to import. But you need
to use fully qualified name every time when
you are accessing the class or interface.
• It is generally used when two packages have
same class name
//save by [Link]
package pack;
public class A{
public void
msg()
{[Link]
ntln("Hello"); Output: Hello
}
}
//save by [Link]
package mypack;
class B{
public static
void
main(String
args[]){
pack.A obj =
new
pack.A();
Compiling and Running
• If you are not using any IDE, you need to follow the
syntax given below:
javac -d directory javafilename

Ex: javac -d . [Link]

Note:
 -d switch specifies the destination where to put
the generated class file;
 d:/abc;
 want to keep the package within the same directory, you
can use . (dot)]
• For java package program:
– You need to use fully name e.g.
qualified [Link] etc to run
the class.
• To Compile: javac -d . [Link]

• To Run: java [Link]


Subpackage in java
package [Link];
class Simple{
public static void main(String
args[]){
[Link]("Hello
subpackage");
}
}
To Compile: javac -d . [Link]
To Run: java [Link]

Output: Hello
subpackage
Advantage of Java Package

1) Java package is used to categorize the classes


and interfaces so that they can be easily
maintained.

2) Java package provides access protection.

3) Java package removes naming collision.


Exercise

You might also like