We have come this far. It was an easy ride till now. I think it’s time to go a bit deeper. We need to clear out how java works exactly. So let’s start scratching the surface a little bit. First thing we need to see is what happens when we start our program.
When we give the command “java Test” many things happen before our main method is called. First of all the virtual machine is loaded. Then a special module called “classloader” starts loading classes. It loads a few other classes before it loads our main one. Then, our main class is loaded and the main method is called. There is where we take over.
As you can see in the main api page of the documentation there are three panes. The top left one is the package selector, the bottom left is the class selector and the main one is the actual viewer. So, as you can see, there are many classes shipped with the default JDK version ov java. There are many many more on the J2EE (we won’t be saying anything more about that for now). When the classloader starts, it loads all the classes from the package java.lang. If we want any other class from any other package a simple “import” is due. For instance, if we want the class Vector from the java.util package we need to issue an “import java.util.Vector“. All imports should be done before the beginning of our public class. If we want all the classes from the package we can use an “*”. This is not prefered as we bloat the classloader with many uneeded things. Here is what a class with a couple of imports would look like.
import java.util.Vector;
import java.io.*;
public class Test{
public static void main(String args[]){}
}
Both ways are up there. Now packaging is a way to group your classes together. Take a peak at the classes in the java.util package. They are all in there because they are all utility classes. So, when an import is issued the class loader takes over and loads the class into the memory.
Now let’s go back to the first course. Rememer setting the PATH environment variable? This was to include all the binary files of java so we are able to type “java” or “javac” on the console and it is recognizable. There is one more variable that java defines on startup, CLASSPATH. This actually has the paths of the main jar files (in the lib directory of the jdk installation) and the current directory. When i say current directory i mean the directory where the “java” command is issued. This is why, when on the previous crash course we had the class and the subclass we didn’t give any import command. Any class that is on the current directory and the java.lang package does not need an import.
Now, what if we want to create our own package? Well it is fairly easy. Let’s say that you create a folder on your desktop called “test” and you change directory within a console to it. This is your running directory. Now, create a java file called “Main.java”. Then create a subfolder called “mypack” and a java file in there called “Sub.java”. Now, open both files with a text editor and put the following in there:
Main.java:
import mypack.Sub;
public class Main {
public static void main(String args[]){
(new Sub()).printMessage();
}
}
Sub.java:
package mypack;
public class Sub {
public void printMessage(){
System.out.println("Package message...");
}
}
There are some points i’d like to make:
- Notice the “package mypack;” statement on the top of the sub class. This is how we declare that this class belongs to this package.
- Notice the “import mypack.Sub” on the top of the main class. This is how we import our class.
- The declarations within the class should go a)imports, b) packages c) class decleration. This means that if in the sub class we wanted to import we would do so before the “package” declaration.
- The package is relevant to the classpath. If there was a second folder within the folder mypack called “subpack” and a class called “SubSub.java” we would import like this: “import mypack.subpack.SubSub” and we would declare the package “package mypack.subpack”. As you can see it all starts from the “test” folder of the desktop, since it is by default included in the classpath.
Start playing around with packages and make sure you take a good look on the ones provided by the JDK since we are going to need alot of those 😉
As always, for any problem report back here.
Nice tut 🙂
thx for it 😉