You are on page 1of 4

Java Package

 A java package is a group of similar types of


classes, interfaces and sub-packages.
 Package in java can be categorized in two
form, built-in package and user-defined
package.
 There are many built-in packages such as
java, lang, awt, javax, swing, net, io, util, sql
etc.

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.

Simple example of java package

The package keyword is used to create a package in java.

Here, javac –d . Simple.java in this


The -d is a switch that tells the compiler where to put the class line compiler create folder name
file i.e. it represents destination. The . represents the current mypack in which Simple.class is
folder. stored
How to access package from another package?

There are three ways to access the package from outside the package.

1. import package.*;
2. import package.classname;
3. fully qualified name.

1) Using packagename.* 2) Using packagename.classname

If you use package.* then all the classes and interfaces If you import package.classname then only declared
of this package will be accessible but not sub class of this package will be accessible.
packages.

The import keyword is used to make the classes and


interface of another package accessible to the current
package.

3) 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 e.g. java.util and
java.sql packages contain Date class.
Java Static Import
The static import feature of Java 5 facilitate the java
programmer to access any static member of a class directly.
There is no need to qualify it by the class name.
Advantage of static import:
 Less coding is required if you have access any static
member of a class oftenly.
Disadvantage of static import:
 If you overuse the static import feature, it makes the
program unreadable and unmaintainable.
What is the difference between import and static import?

The import allows the java programmer to access classes of a package without package
qualification whereas the static import feature allows to access the static
members of a class without the class qualification. The import provides
accessibility to classes and interface whereas static import provides
accessibility to static members of the class.

What is System.out.println()? Here String is variable like int a=10;

To find the length of S in given code, what you have to write in place of Ans??
1. Here, S is static variable of type
String present in Test class
2. So, static variable is access using
class_name.static_variable_name as
Test.S
3. To find length of static variable S,
length() method is used of class String
where S is an object and we know
object can access method as
S.length()
Same concept is used in System.out.println() as:
1) Compiler by default imports java.lang
package which includes System class.
2) out is a (public static final) variable of type PrintStream
declared in the System class. PrintStream is another class.

3) println() is an overloaded method defined in the


PrintStream class.
Access Specifires: consider below two packages blue & brown

Refer green Arrows for access with respect to Actor class in below E.g.

Default (only brown package can access Actor


Public (all classes can access Actor Protected (package brown and inherited
not even accessible by inherited class villain)
class) classes can access Actor)

Private (only members in Actor class can


access Actor class)

You might also like