HowTo Java 3: Packing Packages........
This time I`ll try to pen down how to create package and how to use it.
NAIL DOWN 1:==============================================
The first thing wat I want to hammer down in ur head is that while creating package ALWAYS REMEMBER THAT THE PACKAGE NAME WILL BE THE SAME AS THE DIRECTORY NAME WHICH CONTAINS IT.
eg.
Say urs directory structure is like
c:\javasource\test.java
N u now wanna to create a package .Urs package name should must be "javasource"
NAIL DOWN 2:
===================================================
While creating package in a java coding the Java source for each class must appear in a file that contains a package statement as its first non-comment statement,
Dint get it...???
K...
See the snippet...
package HasimsPackage;
public class HasimsClass1
{
public static void output()
{
System.out.println("i am c:\\HasimsPackage\\HasimsClass1");
}
}
Now for sake of understanding say we would like to create a packege named as HasimsPackage which will contain two classes.Oh sorry u dont know yet wats a package....!!!
K..
[DEFINITION OF PACKAGE:==========================
A package consists of one or more .class files that occupy a single directory]
Yah,now come back to the point.The two classes u say,HasimsClass1 and HasimsClass2.
NAIL DOWN 3:===============
If you want both classes to be accessible to others, then each class must be declared as public in its own file of the same name(dont laugh...!! obviously it should be),in a directory named HasimsPackage. Yah,this can be a subdirectory.
So I just want u to recollect the things till now.First,if u wanna a package,check where u coding,i mean the path of ur java file which u r coding.Second,just add
Well,now create the second class as
package HasimsPackage;
public class HasimsClass2
{
public static void output()
{
System.out.println("i am c:\\HasimsPackage\\HasimsClass2");
}
}
NAIL DOWN 4:=====================
If we have more than one public class in the same file then...??
Yah good question.Only one class can be callable.Not others.
Ok...Now create a Test directory anywhere and carry on coding as follows...
//Test\Test.java
import HasimsPackage.*;
class Test
{
public static void main(String[] args)
{
HasimsClass1.output();
HasimsClass2.output();
}
}Now compile it from command line
javac -verbose -classpath c:\;. Test.java [supposing the directory is in c:\]
[Verbose is doing noting but giving you a live commentary wats happening in the field...-classpath telling javac to check for packages in c:\ and in current directory.well the output below is self explanatory.its checks test.java.it gets external class( HasimsClass1 and 2)
It loads them.It creates Test.class.it creates HasimsClass1.class and 2.class.....and et all....
C:\Test>javac -verbose -classpath c:\;. Test.java
[parsing started Test.java]
[parsing completed 47ms]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/lang/Object.class)]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/lang/String.class)]
[checking Test]
[loading c:\HasimsPackage\HasimsClass1.java]
[parsing started c:\HasimsPackage\HasimsClass1.java]
[parsing completed 0ms]
[loading c:\HasimsPackage\HasimsClass2.java]
[parsing started c:\HasimsPackage\HasimsClass2.java]
[parsing completed 0ms]
[wrote Test.class]
[checking HasimsPackage.HasimsClass1]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/lang/System.class)]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/io/PrintStream.class)]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/io/FilterOutputStream.class)]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/io/OutputStream.class)]
[wrote c:\HasimsPackage\HasimsClass1.class]
[checking HasimsPackage.HasimsClass2]
[wrote c:\HasimsPackage\HasimsClass2.class]
[total 313ms]
]
Then run it....
C:\Test>java -classpath c:\;. Testi am c:\HasimsPackage\HasimsClass1
i am c:\HasimsPackage\HasimsClass2
=================================
Wats the need to include the "." current directory while running...?
Bcz its need to load the main() method of the starting class(here Test)
Kkkkkkkkkkk.........Its enoughhhhhh.......If I start talking bout classpath......Oh nooo......I`ll pen down....sorry.... key down it later...
Its time to lunch.
:)
Hasim
Labels: Technical_HowTo_Java
0 Comments:
Post a Comment
<< Home