静态方法的调用不需要声明对象调用,当静态方法和调用静态方法的方法在同一个类里面的时候,可以直接写静态方法调用,如:
package static_method_test;
public class Static_method {
public static void main(String[] args){
//Static_method Sm= new Static_method();
//Sm.helloworld();
helloworld();
}
public static void helloworld() {
// TODO Auto-generated method stub
System.out.println("hello,world!");
}
}
这个静态方法和 main 方法在同一个类内,所以可以在 main 方法里面直接调用 helloworld(),当然也可以先声明一个对象,再对其调用,但是如果静态方法和调用静态方法的方法不在同一个类内,那么需要用:类名.方法 的方式调用静态方法,如:
package static_method_test;
public class Test2 {
public static void helloworld(){
System.out.println("hello,world");
}
}
package static_method_test;
public class Static_method {
public static void main(String[] args){
//Static_method Sm= new Static_method();
//Sm.helloworld();
Test2.helloworld();
}
}
如果两个类不在同一个包内,还需要 import 那个包