static的方法调用是不是只有:
1.类的示例.静态方法
2.类名.静态方法
为什么我在书上还看到直接在另一类里调用静态方法
代码如下:
- Java code
class A{int i;}public class VarArgs{ static void f(Object[] x){ for(int i = 0; i < x.length; i++) System.out.println(x[i]);}public static void main(String[] args){ f(new Object[] { new Integer(47), new VarArgs(), new Float(3.14), new Double(11.11) }); f(new Object[] {"one", "two", "three" }); f(new Object[] {new A(), new A(), new A()}); }}
就那个f 前边为什么不要VarArgs呢???
------解决方案--------------------
main()方法和f()同在VarArgs类内 可以不加类名 直接引用静态方法f()
------解决方案--------------------
------解决方案--------------------
你可以看看这个程序。class.forname()方法是将制定的路径的类的.class文件加载到虚拟机
package com.statics.test;
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
Class.forName("com.statics.test.StaticInit");
System.out.println("======================分割线===========================");
f();
//瞧私有方法
privateFunction();
}
public static void f(){
System.out.println("因为f()方法是在Test内部被调用的。因此可以直接用f()来调用该函数。使用Test.f()同样也可以\r\n" +
"注意:main函数是在Test的内部的。为了能让你能够更好的理解。我们再里面调用私有的方法试试就知道了");
}
private static void privateFunction(){
System.out.println("private method");
}
}
class StaticInit{
static{
System.out.println("我被初始化了");
}
}
------解决方案--------------------
楼主没注意到main方法是在VarArgs中的吧,故可直接拿来用了。
------解决方案--------------------
静态方法可以直接调用静态方法
------解决方案--------------------
1L说的很对