Chapter 5 - Packages
Chapter 5 - Packages
class Simple {
public static void main(String[] args) {
System.out.println("Welcome to package");
}
}
public class A {
public void msg() {
System.out.println("Hello"); >javac –d . A.java
}
}
//save by B.java
package mypack;
import pack.*;
class B {
public static void main(String[] args) {
A obj = new A();
obj.msg();
} >javac –d . B.java
} >java mypack.B
Output: Hello
How to access package from another
package? …
2) Using packagename.classname:
− If you import package.classname then only declared class of
this package will be accessible.
− Example of package by import package.classname
//save by A.java
package pack;
public class A {
public void msg() {
System.out.println("Hello");
}
}
//save by B.java
package mypack;
import pack.A;
class B {
public static void main(String[] args) {
A obj = new A();
obj.msg();
}
}
Output: Hello
How to access package from another
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.
How to access package from another
package? …
• Example of package by using import fully qualified name:
//save by A.java
package pack;
public class A {
public void msg() {
System.out.println("Hello");
}
}
//save by B.java
package mypack;
class B {
public static void main(String[] args) {
pack.A obj = new pack.A(); //using fully qualified name
obj.msg();
}
}
Output: Hello
How to access package from another
package? …
• If you import a package, all the classes and interface of
that package will be imported excluding the classes and
interfaces of the subpackages. Hence, you need to import
the subpackage as well.
• Note: Sequence of the program must be package then
import then class.
Sub-package in Java
• Package inside the package is called the sub-package. It
should be created to categorize the package further.
• Let us take an example, Sun Microsystem has defined a
package named java that contains many classes like
System, String, Reader, Writer, Socket, etc.
− These classes represent a particular group e.g. Reader and Writer
classes are used for Input/Output operation, Socket and
ServerSocket classes are used for networking, and so on.
− So, Sun Microsystem has subcategorized the java package into
subpackages such as lang, net, io, etc. - and they put the
Input/Output related classes in io package, Server and
ServerSocket classes in net package and so on.
Example of Subpackage
//save Simple.java
package com.test;
class Simple {
public static void main(String[] args) {
System.out.println("Hello subpackage");
}
}
To Compile: javac -d . Simple.java
To Run: java com.test.Simple
• To Run:
− To run this program from E:\source
directory, you need to set classpath of
the directory where the class file
resides.
E:\sources> set classpath=C:\classes;.;
E:\sources> java mypack.A
How to send the class file to another
directory or drive? …
• Another way to run the above program by -classpath
switch of Java:
− To run this program from E:\sources directory, you can use -
classpath switch of java that tells where to look for class file.
− For example:
E:\sources> java -classpath C:\classes mypack.A