How does Java Hello World work without an object instance?

I'm new to Java and I'm confused about something:

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

public class HelloWorld { public static void main (String args[]) { System.out.println ("Hello World!"); } } 
+7
java
source share
6 answers

Any variable or method declared static can be used regardless of the 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 is done because the variable INT_VALUE declared static (for example, the main method).

Try compiling this class with the previous class:

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

This succeeds because the variable INT_VALUE is both static and public . Without going into details, it is usually useful to avoid public variables.

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 an instance of the object from the HelloWorld class is missing. 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 ); } } 
+10
source share

This does not instantiate HelloWorld because main is a static method. Static methods (and static fields) are associated with the type, and not with a specific instance of the type.

See the Java Tutorial page for static / instances for more details, as well as this Question (among other things).

+14
source share

A larger version of OO will look like this:

 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 expected. He creates an instance of the HelloWord class, and then asks him to do something. For learning OO, I find it more intuitive, and (for reasons that I will not do here), I try to avoid static methods when writing my own classes (in short - problems with threads / shared state, etc.).

+9
source share

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

+4
source share

Later, if you want to use any HelloWorld methods that are not static, you must create an instance of HelloWorld in the main method (main will not execute again because it is not a constructor).

0
source share

Your build system will link the program entry point to the "main" class routine. Only one class can have a β€œmain” procedure.

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

-one
source share

All Articles