如果在编译时解析静态方法对象实例如何能够调用静态方法?如何调用静态方法在java中解析?
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]);
}
}
运行StaticCall类之后的输出是
类StaticCall调用类MyMainClass的静态方法
这是StaticCall主
作为静态字段和方法属于类对象如何是一个能够调用静态方法的实例吗? 何时创建了Class对象,它是第一次访问它的任何字段或方法吗?
编译器知道类型。 –
@Nambari因为编译器知道类型。 –