2
public class Test {
  public static void main(String[] args) {
    Test test = null;
    test.func();
  }

  static void func(){
    System.out.println("Hello!!");
  }      
}

Why this program is getting executed successfully?

Flown
  • 11,480
  • 3
  • 45
  • 62
Saheb
  • 51
  • 6

2 Answers2

3

This is because static methods are not related to instances. Compiler internally convert this and call Test.func()

Rahul
  • 309
  • 1
  • 11
0

You call a static method. Static methods are invoked on Classes.

The call is like this Test.func().

Tip: In your code when you call static methods. Call them Class.method() not on object.