[thelist] Java

Santilal Parbhu santilal at scorpioneng.co.nz
Thu Jul 24 00:21:10 CDT 2014


Hi

I am trying to learn Java and I am hoping that someone can help me get my
head around it.  Below is the Hello World example.  I am finding it very
hard to understand despite two days research on the internet.

/** Comment
 * Displays "Hello World!" to the standard output. 

 */
public class HelloWorld {
      String output = "";
      static HelloWorld helloObj;  //Line 1 

      public HelloWorld(){
            output = "Hello World";
      } 

      public String printMessage(){
            return output;
      } 

      public static void main (String args[]) {
            helloObj = new HelloWorld();  //Line 2
            System.out.println(helloObj.printMessage());
  }

}

The first line creates a new class - correct??
Then the variable "Output" is initialised as a string.
I think static HelloWorld helloObj creates a new class within a class.  Why
would you do this?
public HelloWorld(){ - I have no idea what this does??
Then it looks like output is updated to contain "Hello World".  Why not just
initialise this to "Hello World" above??
I think the next bit calls the printMessage() method, but I don't know why
it is preceded with "Public String".
I think the "return output" somehow tells the method to print the contents
of output.
Is the last bit the main method of the class.  But I don't understand the
construction at all.  What is the helloObj = new HelloWorld do. 
That last line prints the output, but I thought that printMessagge above did
this.

Can anyone point me to a good resource to help me anwser these questions?

Thanks.

Santilal Parbhu


More information about the thelist mailing list