OOPS in JAVA 21
Package in Java
A package can be defined as a group of similar types of classes, interface, enumeration and sub-package.
Package are used in Java, in-order to avoid name conflicts and to control access of class, interface and enumeration etc.
Using package it becomes easier to locate the related classes.
Package are categorized into two forms:
1. Built-in Package:-Existing Java package for example java.lang, java.util etc
2. User-defined-package:- Java package created by user to categorized classes and interface
Creating a package:
Creating a package in java is quite easy. Simply include a package command followed by name of the package as the first statement in java source file.
Example:
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package.");
}
}
The above statement create a package called mypack.
Java uses file system directory to store package. For example the .class for any classes you to define to be part of mypack package must be stored in a directory called mypack.
How to compile java package:
If you are not using any IDE, you need to follow the syntax given below.
javac -d Destination_folder file_name.java
Example:
javac -d . Simple.java
The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use .(dot).
How to run java package program:
You need to use fully qualified name e.g. mypack.Simple etc to run the class.There are three ways to access the package from outside the package
1. import package.*;.
2. import package.classname.
3. fully qualified name.
import keyword
import keyword is used to import built-in and user-defined packages into your java source file. So that your class can refer to a class that is in another package by directly using its name.
1. Using packagename.*:
If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
2. Using packagename.classname:
If you import package.classname then only declared class of this package will be accessible.
//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
3. Using fully qualified name (But this is not a good practice.):
It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.
//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
NOTE: If you import a package, subpackages will not be imported.
Static import:
static import is a feature that expands the capabilities of import keyword. It is used to import static member of a class. Using static import, it is possible to refer to the static member directly without its class name.
Disadvantage of static import:
1. Less coding is required if you have access any static member of a class oftenly.
Advantage of static import:
1. If you overuse the static import feature, it makes the program unreadable and unmaintainable.
There are two general form of static import statement:
1. The first form of static import statement, import only a single static member of a class.
Syntax:
import static package.class-name.static-member-name;
Example:
import static java.lang.Math.sqrt; //importing static method sqrt of Math class
2. The second form of static import statement,imports all the static member of a class.
Syntax:
import static package.class-type-name.*;
Example:
import static java.lang.Math.*; //importing all static member of Math class
Example without using static import:
public class Test
{
public static void main(String[] args)
{
System.out.println(Math.sqrt(144));
}
}
Output : 12
Example using static import:
import static java.lang.Math.*;
public class Test
{
public static void main(String[] args)
{
System.out.println(sqrt(144));
}
}
Output : 12
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )