le sidebar
back to...
 main
section 3: to dissect the beast

Back | Next section unfinished

so how does it work?
Let's go for a brief flashback here...
//cut here-------
public class HelloWorld
{
  String message;

  public HelloWorld()
  {
    message = "Hello, World!";
  }

  public static void main(String args[])
  {
    HelloWorld hello1 = new HelloWorld();
    System.out.println(hello1.message);
  }
}
//cut here-------

When I put this little program together, I made it a bit more complicated than it needed to be in order to demonstrate the Object-Oriented aspect of Java. This basically means that the entire program is centred around an object of the class HelloWorld.

An object, in Java, is a notional "structure" or entity within a running application or applet. Objects contain data as well as methods, which are the various "actions" which the object can perform.

Each object belongs to a class. This is modelled after the real world; for example, by analogy, you are an object of the class "Human Being". (At least, I hope you are!) The point where the analogy breaks down is this: Java objects can have no more data, and can perform no other methods, that what is defined by their class.

Coming back to the program, we can see in the very first line that HelloWorld is a public class. This line, the class declaration, is usually placed before any other program statements except for imports (which we shall discuss later). The use of the keyword public tells the compiler that the class should be accessible from "outside" - i.e. by other classes if need be. Following this is an opening curly brace, which is in fact very important: it tells the Java compiler where the class begins. We're expected to tell the compiler where the class ends too, and you can see another curly brace - the "mate" of the first one - at the very end of the program.

Then comes the line:
String message;
This is actually a variable declaration which tells us (and Java) that objects of the HelloWorld class can contain one item of data: a String which is named message. Strings in Java are simply sequences of letters, numbers and other characters - in other words, we can use them to contain words, phrases or sentences.

After this is the constructor method, public HelloWorld(). A constructor method is one which a new object always performs (once only) before doing anything else; Java recognises this by the name of the method, which is the same as the name of the class. Notice the curly braces here; all methods (except abstract ones, but that's getting too far ahead of ourselves) will have a pair of these to define where the method begins and ends. Within them is the statement
message = "Hello, World!";
When a new object of the class HelloWorld is first created, its only data item, message, contains no data. This statement gives it some data - a sentence.

Finally, we have the main method. When a Java program is run, this method is the "entry point" - it is the first method to activate, and all others must have been activated (directly or indirectly) by it. Note the curly braces again. Note also that the method is public, and static, and void. We'll go more into these things, as well as the String args[] (I recently found out that a 3rd year student of my acquaintance doesn't know what these are!), later.

Two statements appear here. The first one is the important one:
HelloWorld hello1 = new HelloWorld();
This creates a new object of the class HelloWorld and names it hello1. Note the opening and closing brackets; these serve to indicate that a method (in this case the constructor) is being called.

The second statement activates the method System.out.println(). This is a common method which is provided by Java for the use of us programmers; any String data item placed within the brackets will have its contents displayed on the screen. Of course, the message is probably what we want displayed, but whose message? We can't very well say "HelloWorld's", as that would be like replying to the question "Whose bag is this?" with the answer "A human being's". Thus we have to name the object whose message is to be displayed - hello1. The proper way to refer to a data item belonging to an object, in Java, is as follows: [object_name].[item_name] - something similar to when we say in English "[owner]'s [item]".

Well, that's about it for now. I'll try to add more stuff ASAP. Until then, take time to smell the coffee, folks...
Back | Next section unfinished