Sunday, 29 January 2017

PACKAGES IN JAVA

A folder that is linked with java class is called package. It is used to group related classes, interfaces and enums. It is also used to separate new classes from existed classes. So, by using package we can create multiple classes with same name, also we can create user defined classes with predefined class names. By default one predefined package is imported for each and every java program and whose name is java.lang.*. To create a package we have a keyword called “package”. Syntax –

package <package name>;
For example – package p1;

Rules on package statement – package statement should be the first statement in a java file.

Default package – Package statement is optional. If we define a class without package statement, then that class is said to be available in “default package” i.e., current working directory.

Example –
Package p1;
Class Example{
public static void main(String[ ] args){
System.out.println(“Hello java”);
}

Note – Compiler does not create package physically in current working directory just with javac command. Packaged classes must be compiled with “-d” option to create package physically.

Output –
javac –d . Example.java
java p1.Example
Hello java

With this command, compiler creates package “p1” eith “Example.class” and places it in current working directory. Operator “.” Represents current working directory. Example javac –d C:\test

“-d” functionality – Its actual functionality is creating package with the name mentioned in java file and moving all class files into that package and finally storing that package in the given path.

Packaged class code changed by compiler
After compilation, compiler replaces class name and its constructor name and with its packagename.classname. It is called as fully qualified name.

Example.java
Package p1;
Class Example{
public static void main(String[ ] args){
System.out.println(“Hello java”);
}

Example.class
Class p1.Example extends java.lang.Object{
P1.Example()
{super();}
public static void main(String[ ] args){
System.out.println(“Hello java”);
}
}

How to access other package from our package class.
There are 2 ways –
·        By using fully qualified name or
·        By using “import” keyword

Understanding “import” keyword
import keyword is used to access other package members from this package classes. Actually it doesn’t import other package members into this package instead it shows the path of other package member to compiler and jvm. Syntax –
import p1.*;
          or
import p1.Example;

Note – import statement must be placed before all class definition, and after package statement and access one package to another package one public member else CE.

Note – If user class is also defined in same package, import statement and fully qualified name is optional. If user class is taken from another package, either import or fully qualified name must be used else it leads to CE : cannot find symbol.

What is the benefit we get in using import statement over fully qualified name ?
If we do not use import statement, we must use package name wherever we are using class name or constructor. This code is considered as redundant code and also this code is not readable. To solve this problem SUN introduced “import” concept so, if we use import statement we don’t need to use package name in referring class name and its constructor.

Package p2;
Class Sa{
Public static void main(String[ ] args)
{
p1.A a1 = new p1.A();
p2.A a2 = new p2.A();
}
}
The above code is not readable


Package p2;
Import p1.*;
Class Sa{
Public static void main(String[ ] args)
{
A a1 = new A();
A a2 = new A();
}
}
The above code is readable


Using sub package members
We must import sub package separately to access its members. Since by importing parent package, members are not imported vice versa is also not possible.

Example –
package p1.p4;
public class D{
 public void m1()
{
System.out.println(“D m1”);
}
// Test,java
Package p2;
import p1.*;
import p1.p4.*;
public class Test{
public static void main(String[] args)
{
D d = new D();
}
}

No comments:

Post a Comment