7

I started to write my first Java program.

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

The program runs fine just with the above code. But according to my OOP knowledge, a class is only an abstract concept and doesn't come to life untill you create an object of its kind. And then, through that object we call the methods/functions inside the class.

But in this particular example, it seems that main method is called even without creating an object of the class HelloWorldApp

Is the object created somewhere else? If so, how does that portion of the code know my class name HelloWorldApp?

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125

3 Answers3

10

It is because it is static method, and for that it doesn't need to create instance

JVM will load the HelloWorldApp class and it will invoke static method on it, And since it is public JVM (being external ) can access this method


Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Does that mean I can call around any static functions inside any other class? Doesn't it downplay the concept of encapsulation? – Pavan Manjunath May 16 '12 at 11:49
  • yes you can, static fields/methods are associated with class (per class loaded and not with the Object) , if they are public you can do it – jmj May 16 '12 at 11:51
  • 2
    Encapsulation isn't a rule that's enforced by the compiler but rather a principle that programmers use to try to create better code. The language and compiler can help us achieve encapsulation but doesn't force it on us. Even a pure object oriented language like Smalltalk doesn't force you to follow encapsulation. In Smalltalk, however, classes are themselves real objects so you can send them messages that they respond to. You can even inherit them in subclasses and have subclasses invoke super. – David Buck May 16 '12 at 12:12
3

Starting point for a java application (not always ) is this method

public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }

When you do java className it will go and check if the class has a main method, since its static it can be called without creating an instance.

If there is no main method or main method is there but not with the same signature it will throw you a RuntimeException stating main method not found.

Don't forget to read A closer look at main method.

off topic:

Extending the same idea, you don't need an instance of a class to refer its static method and fields.

public class MyClass {
  public static int NUMBER = 10;

  public static void String sayHello(){
    return "Hello";
  }

  public void String sayBye(){
    return "Bye";
  }

  public static void main(String[] args){
    System.out.println(NUMBER); // No need for object 
    System.out.println(sayHello()); // No need for object 
    System.out.println(new MyClass().sayBye()); // sayBye is not accessible at class level have to have an object of MyClass to access sayBye
  }
}

If same is called in some other class then it may look like:

public class MyClassCaller {
   public static void main(String[] args){
    System.out.println(MyClass.NUMBER); // No need for object just refer the class
    System.out.println(MyClass.sayHello()); // No need for object just refer the class
    System.out.println(new MyClass().sayBye()); // sayBye is not accessible at class level have to have an object of MyClass to access sayBye
  }
}

A nice discussion on usage/overusage of static methods

Community
  • 1
  • 1
mprabhat
  • 20,107
  • 7
  • 46
  • 63
1

main method is static which does not needs an instance

Satya
  • 8,693
  • 5
  • 34
  • 55