Java Packages
Java Packages
Built-in Packages
The Java API is a library of prewritten classes, that
are free to use, included in the Java Development
Environment.
The library contains components for managing
input, database programming, and much much
more. The complete list can be found at Oracles
website: https://docs.oracle.com/javase/8/docs/api/
.
The library is divided into packages and classes.
Meaning you can either import a single class
(along with its methods and attributes), or a whole
package that contain all the classes that belong to
the specified package.
Example
Using the Scanner class to get user input:
import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
Adding a class to a Package : We can add more classes to a created package
by using package name at the top of the program and saving it in the package
directory. We need a new java file to define a public class, otherwise we can
add the new class to an existing .java file and recompile it.
Import a Package
There are many packages to choose from. In the
previous example, we used the Scanner class from
the java.util package. This package also contains
date and time facilities, random-number generator
and other utility classes.
To import a whole package, end the sentence with
an asterisk sign (*). The following example will
import ALL the classes in the java.util package:
Example
import java.util.*;
User-defined Packages
To create your own package, you need to
understand that Java uses a file system directory to
store them. Just like folders on your computer:
Example
└── root
└── mypack
└── MyPackageClass.java
Execution steps
1.Save the file as MyPackageClass.java, and
compile it:
C:\>javac MyPackageClass.java
2.Compile the package
C:\>javac -d . MyPackageClass.java
This forces the compiler to create the
"mypack" package.
The -d keyword specifies the destination for
where to save the class file. You can use any
directory name, like c:/user (windows), or, if
you want to keep the package within the same
directory, you can use the dot sign ".", like in
the example above. The package name should
be written in lower case to avoid conflict with
class names.
3.When we compiled the package in the
example above, a new folder will be created,
called "mypack". To run the
MyPackageClass.java file, write the following:
C:\>java mypack.MyPackageClass
Program 2:
import letmecalculate.Calculator;
public class Demo{
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}
Example
package package_1;
package package_2;
import package_1.ClassOne;
import package_2.ClassTwo;
javac Testing.java
java Testing.java
1. import package.*;
2. import package.classname;
3. fully qualified name.
1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be
accessible but not subpackages.
The import keyword is used to make the classes and interface of another package
accessible to the current package.
import java.util.*;
import pacakage_1.*;
2) Using packagename.classname
If you import package.classname then only declared class of this package will be
accessible.
import package_1.ClassOne;
import java.util.Scanner;
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.
//save as A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save as B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully
qualified name
obj.msg();
}
}
How to Hide Class in a Package?
Hiding classes:
When we import a package using astric(*),all public classes are imported.however ,we may
prefer to “not import”certain classes.i.e,we may like to hide these classes from accessing
from outside of the package.such classes should be declared”not public”.
EX:
package p1;
public class X
Body of X
class Y
{
Body of Y
Here, the class y which is not declared public is hidden from out side of the package p1. this
class can be seen and used only by other classes in the same package note that a java
source file should contain only one public class and may include any number of non public
classes.
Now ,consider the following code ,which imports the package p1 that contains classes X and
Y:
import p1.*;
Java compiler would generate an error message for this code because the class Y,which
has not been declared public,is not imported and therefore not available for creating its
objects.
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.
Example:
import static java.lang.System.*;
class StaticImportExample{
public static void main(String args[]){
out.println("Hello");//Now no need of
System.out
out.println("Java");
Output:Hello
Java
}
}
With the help of static import, we can access the static members of a class
directly without class name or any object. For Example: we always use sqrt()
method of Math class by using Math class i.e. Math.sqrt(), but by using
static import we can access sqrt() method directly.
Example 2
// Java Program to illustrate
// calling of predefined methods
// with static import
import static java.lang.Math.*;
class Test2 {
public static void main(String[] args)
{
System.out.println(sqrt(4));
System.out.println(pow(2, 2));
System.out.println(abs(6.3));
}
}
Output:
2.0
4.0
6.3