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
package forest;
import java.util.*;
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.
package forest;
import java.util.*;
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.
import forest.Tiger;
public class Animal
{
public static void main(String args[])
{
Tiger t1 = new Tiger ();
t1.getDetails("Everest", 50);
}
}
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.
import forest.*;
To work with the above statement, remove the source file Tiger.java file from C:\snr directory; else use as import forest.Tiger.
i like him
well..i searched on youtube too..but t was boring..but here in, you ha explained very nice n deep enough..not very deep..which was very good jut understand..thank you sir..
very nice explained by you and suitable example.
it really clear the fundamental.
sir,what is the reason for deleting .java file from C:snr when we try to acceess with ‘ * ‘ operator?assuming there are two or more .class files in the package.
Reason not given in Java documentation also.
The subpackages always are not imported when you try to import the super package.So..You must specify the exact file to import rather than the * symbol.
sir,while executing ” snr>javac -d.Tiger.java ” it’s giving an error that file not found? whats the problm?
Your command is wrong. Gice
javac -d Tiger.java
After -d there is no dot.
sir,
1.can we method Overload in Tiger.class??
2.And is it necessary to set classpath after modifying Tiger class each time.(else not compiling)
please explain…
You are talking which posting in way2java. for each modification, you need not set the classpath again. Giving classpath at command-line is a temporary one which works as long as the prompt is open. Once, closed the classpath is lost.
Sir which file we need to delete the Tiger.java or Tiger.class if we delete Tiger.java how the target directory(sumathi) is accessed the Tiger.java in Animal.java file from forest package??
Delete Tiger.java. This is to work with “import forest.*;”. Deletion is not required when you write “import forest.Tiger;”.
Sir, can you please rewrite the 3rd step. I am unable to follow it. The output screen is not present over there.
The other thing is you said that “To work with the above statement, remove the source file Tiger.java file from C:\snr directory” can you please elaborate it. I mean what is the reason behind it? When we are importing forest.* what does the other .java file in parent folder snr has to do with it?
import forest.Tiger;
In the above statement, only Tiger class from forest package is imported (ofcourse, in this case you have only class in the package).
import forest.*;
The above statement imports all the classes present in the forest package. But if you write as above with * mark, the program does not work. To work with, you have got Tiger.java in the C:\snr folder (from where you have compiled earlier). Now delete the Tiger.java file from c:\snr. Then * will work.
But this is elaboration of the process.
What is the reason behind this ?
plz arrange the material in order