类方法也叫静态方法。
基本语法:
访问修饰符 static 数据返回类型 方法名(){ }【推荐形式】 或者 static 访问修饰符 数据返回类型 方法名(){ }
类方法的调用:
类名.类方法名 或者 对象名.类方法名 【前提是满足访问修饰符的访问权限和范围】
package test;public class Main {public static void main(String[] args) {//创建2个学生对象,交学费Stu wty2002 = new Stu("WTY2002");wty2002.payFee(100);//不太规范Stu mary = new Stu("mary");//mary.payFee(200);Stu.payFee(200);//规范方法//输出当前收到的总学费Stu.showFee();//300}}class Stu {private String name;//普通成员//定义一个静态变量,来累积学生的学费private static double fee = 0;public Stu(String name) {this.name = name;}//说明//1. 当方法使用了static修饰后,该方法就是静态方法//2. 静态方法就可以访问静态属性/变量public static void payFee(double fee) {Stu.fee += fee;//累积到fee中}public static void showFee() {System.out.println("总学费有:" + Stu.fee);}}

