Java is a proprietary software language owned by Sun Microsystems and very popular among applications. As an example, Minecraft is written in Java.
Java has its syntax deriving from C and C++. Java is based on a class/method/object architecture. Let’s think about classes as objects containing methods can can be used to modify the contents of this objects or make it interact with other classes or objects. So, when you want to create a java program, you need first to create a class that will run as your main program. Files have the extension .java. Its code is compiled into bytecode (.class files) that can be run on java virtual machines, resolving a lot of cross-platform compatibility problems. In few words, by creating a java application, you can be sure it will run on Windows, Linux and Mac.
At the moment of this post, even I have only few hours of practice of java, but it is quite amazing how you can understand its structure quickly if you have done a bit of C++, C, or C# in your life.
What to do when studying java for the first time of your life and you know nothing about it? First you are going to need a java development environment. By working Ubuntu 10.04 LTS, just pick up the package openjdk-6-jdk in Synaptic package manager.
Once this is installed, let’s get started with a simple example.
By being a programmer, what you *need* to feel when learning a new software language is:
- How to code it with a simple example like “Hello World”
- How to make objects interact
- How to compile it
- How to run it
The example below takes everything in this list into account. Let’s take 3 class A, B and C containing each different methods.
Class A is defined in file A.java.
public class A {
public void Hello() {
System.out.println("Hello World, class A");
}
public static void static_hello() {
System.out.println("Hello World, static class A");
}
}
This class contains two methods, a static and a normal one. A static method can be called even if the class is not compiled (A.class file not present). A non-static method can be called only if class is compiled.
In another class, if you want to call a method inside class A, you first need to create a new object based on class A and then you can call the method:
A a = new A();
a.Hello();
This will print out the message “Hello World, class A”.
Compilation of this file is made by the command javac:
javac A.java
If you want to call the static method of class A from another object, you need to use:
A.static_hello();
This will print the message “Hello World, static class A”. You don’t need to create a new object related to this class when calling a static method. You do not even need to compile the class to use a static method. This has to be called inside another object. By trying to call a method which is not static on a class that is not compiled or not a new object, compilation returns an error tellings that non-static methods have to be used on new objects.
When trying to launch class A independently with a method (done by command java $CLASS_NAME):
~/bin/jar $ java A
Exception in thread "main" java.lang.NoClassDefFoundError: A
Java throws an exception (error message) because class A does not contain a method “main”.
Now let’s imagine two other classes B (saved) in and C.
/* Saved in B.java */
public class B {
public void Hello() {
System.out.println("Hello World, class B");
}
}
/* Saved in C.java */
File Edit Options Buffers Tools Java Help
public class C {
/* Main method that can be called independently */
public static void main(String[] args) {
A.static_hello();
/* A.Hello(); */ /* Error if A not compiled with C */
A a = new A();
B b = new B();
a.Hello();
b.Hello();
}
}
If all the java files are in same repository, compilation is done with command javac:
javac *.java
In case you want to a new class that depends on objects of an external class, you need:
- To set up the environment variable $CLASSPATH with the full directory data to a jar file (java library). Ex:
CLASSPATH=.:$FOLDER/A.jar - Import needed java libraries in the head of the java file of your new class. Ex:
import java.A.*if this library is included in $CLASSPATH
About classes B and C, class C contains a main method, so it can be called independently.
$ java C
Hello World, static class A
Hello World, class A
Hello World, class B
