If static methods are resolved at compile time how is an object instance able to call a static method?
class StaticCall
{
public static void main(String[] args)
{
String arr[]={"Class StaticCall","calls static method of class MyMainClass"};
MyMainClass h=new MyMainClass();
h.main(arr); //How is an instance able to call a static method?
System.out.println("this is StaticCall main");
}
}
class MyMainClass
{
public static void main(String[] args){
System.out.println(args[0]+" "+ args[1]);
}
}
After running the StaticCall class the output is
Class StaticCall calls static method of class MyMainClass
this is StaticCall main
As static fields and methods belong to the Class object how is an instance able to call a static method?
Also when is the Class object created,Is it on first access to any of it’s fields or methods?
It doesn’t. Try this instead
and you will see that the instance is ignored as this is exactly the same as
To extend your example … if you have
then all the following call the same method.