8

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!");  
    }  
}
Janusz
  • 187,060
  • 113
  • 301
  • 369
webkul
  • 2,836
  • 10
  • 47
  • 61

6 Answers6

17

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).

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
11

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
  • 30,436
  • 41
  • 178
  • 315
10

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
  • 268,207
  • 37
  • 334
  • 440
4

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

0

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
  • 5,428
  • 28
  • 42
-1

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
  • 6,772
  • 7
  • 45
  • 71
  • 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 Jul 09 '09 at 10:14