Packages in Java

Packages are used to prevent naming conflicts, to control access, to make controlling/searching and usage of classes, interfaces, enumerations and annotations etc. The package statement is the first line in the source code.
Key point: Lowercase letters are preferred for package names.

Generally, packages are of two types:

1. Built-in: java, lang, awt, etc.
2.User defined packages

Key Point: java.lang package is automatically imported in every Java class.


Built-in packages can used by users by importing them directly. But in some time we need to create our own packages. Those are called user defined packages.


User defined packages:

Ex: 1package mypack;
public class first
{
public static void main(String a[])
{
System.out.println("This is my first package");
}
}
Output: 
>javac -d . first.java(-d(directory) refers to the folder that to place a class file and .(dot) refers to the current folder)
>java mypack.first(First can be run by preceding its package name because its .class file is saved to mypack folder)
This is my first package

Ex: 2 importing one package into another

1st program:
package mypack;(declaration of package)
public class first
{
public void main()
{
System.out.println("This is my first package");
}
}

2nd program:(Importing mypack into the below program)


import mypack.first;

public class second
{
public static void main(String a[])
{
first f=new first();
f.main();
}
}
Output:
>javac -d . first.java
>javac second.java
>java second
This is my first package 

Key Point:All the members of package must be public except instance variable because public members can only be accessed outside the package.


Importing packages are of following types:

1.import package_name.*;(User able to access to all classes and interfaces of that package if user puts *)
2. import package_name.class_name: User able to access to specific class in the package

Sub packages:A package inside another package is called sub package.

declaration:package package_name.subpackage_name;
Ex:
package mypack.pack1;
public class first
{
public static void main(String a[])
{
System.out.println("This is my first package");
}
}
Output:
>javac -d . first.java(We can also compile using '-classpath' like: javac -classpath . first.java)
>java mypack.pack1.first
This is my first package

Note: User can create many number of sub packages like above process.

Ex: package package_name.subpackage_name.subpackage_name1;
Key point: If you import package then sub packages  will not be imported.

There are two approaches in packages:

Package by feature:It can be used to put all classes, interfaces related to one feature.
Ex: about school
com.app.place
com.app.students

com.app.teachers

...
...
...
..etc.
all the package items are related to school feature.

Uses:

Easier code navigation
Higher modularity
Minimizes scope: It allows the some classes could be private
Better growth style: If package becomes too large, it may refactored into in a natural way into 2 or more packages.


Package by layer:

By analogy, package-by-layer style leads to poor results. For example: Imagine a plane. A car's 'implementation' is divided this way by package-by-feature,
  • safety
  • engine
  • fuel system
  • and so on...
According to these lower level categories by package-by-layer,

  • electrical
  • mechanical
  • hydraulic
Key point: Packages provides encapsulation in java. 'Default' specifier considered to be the package-private specifier in java.


Uses of packages:


1. Avoids naming conflicts of classes and interfaces by prefixing classes with package.
2. Provides access control
3. For distributing of a collection of reusable classes, usually in a format known as JAR file.
4. Package hide all the classes and interfaces in a separate sub directory, so that accidental deletion of classes and interfaces will not take place.



Note: In the package directive's absence, those classes and interfaces belong to the unnamed package.

Examples:

1st: first class created in  the package 'mypack'
package mypack;
public class first
{
public void add()
{
System.out.println("This is addition class in mypack package");
}
}
if you compile above program using command javac -d . first.java then it creates a directory and stores first.class in it.

2nd: Third class also created in package 'mypack'

package mypack;
public class third
{
public void add1()
{
System.out.println("This is my second class in mypack package");
}
}
If you compile above program using command javac -d . third.java then java compiler search whether the package with name 'mypack' is already exists or not. if it exits then saves third.class also in the same package 'mypack'.
3rd:
import mypack.first;
import mypack.third;
public class second
{
public static void main(String a[])
{
first c=new first();
c.add();
third d=new third();
d.add1();
}
}
Output:
>javac -d . first.java
>javac -d . third.java
>javac second.java
>java second
This is addition class in mypack package

This is my second class in mypack package

You can use import mypack.*; instead of import mypack.first; import mypack.third; in the last program. But user need to delete(move to other directory) first two .java programs from the current directory otherwise compiler throws error. What really happened is, compiler will search for byte code on those .java files but there is no byte code found in those files results in error message gets displayed. 



package first;

import second.Sample1;

public class Sample {

public static void main(String a[])
{
    Sample1 s=new Sample1();
    s.dis(1);
}


package second;

import first.Sample;

public class Sample1 extends Sample{
public void dis(int i)
{
System.out.println(i);
}
}
How to compile above classes as those are interdependent. 

C:\Users\PE>javac -d . Sample.java Sample1.java

C:\Users\PE>java first.Sample
1

The above one is not a good programming design. Try avoiding this type of bad design.

JAR(Java Archive) files:
It is a file that contains compressed version of audio files, images, .class files. You can create .jar files using the following command. 
c:/>jar cf jarfile_name.jar file_name
c:/>jar cf mypack.jar mypack
(cf-> create file)
To read content from .jar file: c:/>jar tf jarfile_name.jar 
(tf-table view of file)

Manifest file is created automatically, while converting package into jar file.You can extract jar file using the command:c:/>jar xf jarfile_name
(xf-extract file)

User can use the jar file anywhere in the system by setting the right path in the environmental variables in the system.

No comments:

Post a Comment