Java is a friendly language and permits to create our own packages and use in programming. We know earlier, Java allows us to create our exceptions. Creating packages are indispensable in project development where number of developers are involved doing different modules and tasks. We know packages avoid name collision problems. Package naming conventions are very helpful to locate the applications developed by a single individual or team.
Now go on reading User Defined Packages Java.
Steps of creating User Defined Packages Java and using them.
- Create a package with a .class file
- set the classpath from the directory from which you would like to access. It may be in a different drive and directory. Let us call it as a target directory.
- Write a program and use the file from the package.
Let us create a package called forest and place a class called Tiger in it. Access the package from a different drive and directory.
1st Step: Create a package (forest) and place Tiger.class in it.
Let us assume C:\snr is the current directory where we would like to create the package.
C:\snr > notepad Tiger.java
public class Tiger
public void getDetails(String nickName, int weight)
System.out.println("Tiger nick name is " + nickName);
System.out.println("Tiger weight is " + weight);
Order of Package Statement
The above program codingwise is very simple but is important to know the steps of package creation.
public class Tiger
package is a keyword of Java followed by the package name. Just writing the package statement followed by the name creates a new package; see how simple Java is to practice. For this reason, Java is known as a production language.
While create User Defined Packages Java, the order of statements is very important. The order must be like this, else, compilation error.
- Package statement
- Import statement
- Class declaration
If exists, the package statement must be first one in the program. If exists, the import statement must be the second one. Our class declaration is the third. Any order changes, it is a compilation error.
When the code is ready, the next job is compilation. We must compile with package notation. Package notation uses –d compiler option as follows.
The –d compiler option creates a new folder called forest and places the Tiger.class in it. The dot (.) is an operating system's environment variable that indicates the current directory. It is an instruction to the OS to create a directory called forest and place the Tiger.class in it.
Using User Defined Packages Java
After creating the package let us use it.
2nd step: Set the classpath from the target directory.
Let us assume D:\sumathi is the target directory. Let us access Tiger.class in forest package from here.
From the target directory set the classpath following way.
classpath is another environment variable which gives the address of the forest directory to the OS. %classpath% informs the OS to append the already existing classpath to the current classpath that is right now set.
3rd Step: Now finally, write a program from the target directory D:/sumathi and access the package.
The above statement creates a file called Animal.java and write the code in it, say, as follows.
public class Animal
public static void main(String args)
Tiger t1 = new Tiger ();
The compilation and execution is as usual as follows.
D:\sumathi> javac Animal.java
D:\sumathi> java Animal
Following is the output screen.
Importing the Package – Precautions
The following statement raises compilation error.
To work with the above statement, remove the source file Tiger.java file from C:\snr directory; else use as import forest.Tiger.