tags:

views:

531

answers:

6

Hi, I'm a newbie to Java and I'm confused about something:

In the simple hello world program in Java, no object is created so how does the class work in the following example?

public class HelloWorld 
{  
    public static void main (String args[]) 
    {  
        System.out.println ("Hello World!");  
    }  
}
+13  A: 

This doesn't create an instance of HelloWorld because main is a static method. Static methods (and static fields) are related to the type rather than to a particular instance of the type.

See the Java Tutorial page on static/instance members for more details, along with this Stack Overflow question (amongst others).

Jon Skeet
How could you post an answer within seconds?
rahul
Just happened to see it, that's all.
Jon Skeet
Is there a question that you can't answer?
rahul
Oh there are hundreds of them :) This one's a pretty easy one...
Jon Skeet
The question is probably more..."are you doing this as full-time job?" :) But go on that way, your info is really valuable
Juri
@Juri "are you doing this as a full-time job?" - as a Google shareholder I hope not :-)
Brian Agnew
+4  A: 

Static methods like main() can be used without an object.

anon
A: 

Your build system will bind the entry point of the program to the "main" routine of the class. Only one class can have a "main" routine.

"main" is static. this means that it's a "class method". It works without an instance.

Jean-Denis Muys
I suspect you're thinking of C#. In Java, any number of classes can have "main" methods. Mind you, that's true in C# too - you just need to tell the compiler which class should be treated as the entry point.
Jon Skeet
A: 

Later, if you want to use any HelloWorld methods that are not static, you have to create an instance of HelloWorld in main method (main won't be executed again because it's not constructor).

usoban
+9  A: 

A more OO version would look like:

public class HelloWorld {
   public void sayHello() {
      System.out.println("Hello World");
   }
   public static void main(String[] argv) {
     HelloWorld hw = new HelloWorld();
     hw.sayHello();
   }
}

which I suspect is more like what you were expecting. It instantiates a new HelloWord class instance, and then asks it to do something. For learning OO I find this more intuitive, and (for reasons I won't go into here) I tend to shy away from static methods when writing my own classes (briefly - threading issues/shared state etc.)

Brian Agnew
No... a more OO version would look like `ServiceLocator.getService(GreeterFactory.class).getGreeter().greet(WORLD).on(System.out);`. ;)
Christoffer Hammarström
+8  A: 

Any variable or method that is declared static can be used independently of a class instance.

Experiment

Try compiling this class:

public class HelloWorld {
    public static int INT_VALUE = 42;

    public static void main( String args[] ) {
        System.out.println( "Hello, " + INT_VALUE );
    }  
}

This succeeds because the variable INT_VALUE is declared static (like the method main).

Try compiling this class along with the previous class:

public class HelloWorld2 {
    public static void main( String args[] ) {
        System.out.println( "Hello, " + HelloWorld.INT_VALUE );
    }
}

This succeeds because the INT_VALUE variable is both static and public. Without going into too much detail, it is usually good to avoid making variables public.

Try compiling this class:

public class HelloWorld {
    public int int_value = 42;

    public static void main( String args[] ) { 
        System.out.println( "Hello, " + int_value );
    }  
}

This does not compile because there is no object instance from the class HelloWorld. For this program to compile (and run), it would have to be changed:

public class HelloWorld {
    public int int_value = 42;

    public HelloWorld() { }

    public static void main( String args[] ) {
        HelloWorld hw = new HelloWorld();
        System.out.println( "Hello, " + hw.int_value );
    }  
}
Dave Jarvis