//-----------文件名hasStaticMethod.java,程序编号1-----------------public class hasStaticMethod{//定义一个静态方法public static void callMe(){System.out.println("This is a static method.");}}
下面这个程序使用两种形式来调用静态方法。 //-----------文件名invokeStaticMethod.java,2-----------------public class invokeStaticMethod{public static void main(String args[]){hasStaticMethod.callMe(); //不创建对象,直接调用静态方法 hasStaticMethod oa = new hasStaticMethod(); //创建一个对象oa.callMe(); //利用对象来调用静态方法}} 程序3.36两次调用静态方法,都是允许的,程序的输出如下: This is a static method.This is a static method.