Definition:
Package is a collection of similar type of classes and interfaces. It is a good programming practice to keep all related classes and interfaces at separate place, so that they are properly categorized and managed. Package concept is introduced to achieve such task. By placing our classes & interfaces in package a large software project is easier to manage. We have set of inbuilt packages provided by java, like java.lang, java.io etc, and we can also create our own user defined packages. (We will discuss about inbuilt packages in detail in coming chapters.)
Package is getting used in all technologies in different form like in C/C++ we have header files, in dot net DLL files etc.
Java always creates a default package if we do not define it explicitly in our class or interface.
Let’s take a real example and benefits of using package, when a former tries to sell a raw pineapple then it’s really difficult to sale it as it is, but when we go to the super market, there they properly pare it, slice it in small pieces, package it and then sell it, and they finally very easily sale it.
import java.io.*;
import java.util.Scanner;
public class Temp {
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a name: ");
String name = scanner.next();
System.out.println("Name - " + name);
}
}
Output:
Please enter a name:
Alex
Name - Alex
In above code, we are importing all classes and interfaces available inside java.io package in our program (used *), and a single class Scanner from java.util package. Yes, it is possible to either import all classes / interfaces or a specific. Similar to a real example - we always store only those contacts in our mobile which we regularly use, not all.
We also have the option to use the class directly without importing the package, like this:
public class Temp {
public static void main(String args[])
{
java.util.Scanner scanner = newjava.util.Scanner(System.in);
System.out.println("Please enter a name: ");
String name = scanner.next();
System.out.println("Name - " + name);
}
}
Output:
Please enter a name:
Smith
Name - Smith
Now, let’s create our own package. Java has provided a keyword “package” to create user defined package. It should always be the first line in any java program. For example:
package com.p1;
public class Temp1 {
void show()
{
System.out.println("Welcome to dev21century's package concept.");
}
public static void main(String args[])
{
new Temp1().show();
}
}
Output:
Welcome to dev21century's package concept.
If we want to use our class available inside package p1 to other package then we have to make it public. But question arises that we cannot have more than one public class in a single .java file, then how we will use the class outside?
We can have only one public class in a single .java file and name of the .java file must be same as its class name.
This rule is made for implicit compiling – suppose we have 10 .java files and creating object of all of them in 11th .java file, then we need to compile all 10 files first because no class file exists for them, so here java do the implicit compiling (all 10 will be compiled automatically). For example:
program.java
class B
{
}
class A
{
public static void main(String args[])
{
B b = new B();
}
}
Now, if we compile class A then compiler can never predict that B is inprogram.java file. That’s why we have to keep the same name for public classes so that compiler can easily find the class name.
Importing user defined package:
Let’s create one more package com.p2, we will try to import and use class of p1 into p2;
Temp1.java
package com.p1;
public class Temp1 {
public void show()
{
System.out.println("Show method of Temp1 invoked.");
}
public static void main(String args[])
{
new Temp1().show();
}
}
Temp2.java
package com.p2;
import com.p1.Temp1;
public class Temp2 {
public static void main(String args[])
{
Temp1 temp1 = new Temp1();
temp1.show();
}
}
Output:
Show method of Temp1.
Characteristics of java packages:
- No class in java exists without a package, if we do not declare the one then it actually move the class inside a default package.
- rt.jar is a jar file where java keeps all inbuilt packages.
- Operating system treats all packages as a folder.
- It is mandatory for a class or interface to have identical name if they belong to the same package, but name can be repeated if they belong to different package.
- We need to use “import” keyword when we want to embed the class or interface from other package to our class.
- import statement should always be specified before class declaration and after “package” statement.
- java.lang package is a default package, which means it is imported by default in all java programs to make available some common classes like System, String etc.
- package statement should always be the first line in any java program.
No comments:
Post a Comment