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?
This is because static methods are not related to instances. Compiler internally convert this and call Test.func()
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.