- 类方法和普通方法都是随着类的加载而加载,将结构信息存储在方法区;类方法中无this的参数;普通方法中隐含着this的参数
- 类方法可以通过类名调用,也可以通过对象名调用。【规范写法是类名调用】
- 普通方法和对象有关,需要通过对象名调用,比如对象名.方法名(参数),不能通过类名调用。
- 类方法中不允许使用和对象有关的关键字,比如this和super。普通方法(成员方法)可以使用。
- 类方法(静态方法)中只能访问静态变量或静态方法。
- 普通成员方法,既可以访问非静态成员,也可以访问静态成员。
小结:
静态方法,只能访问静态的成员,非静态的方法,可以访问静态成员和非静态成员(必须遵守访问权限)
package test;public class Main {public static void main(String[] args) {D.hi();//ok//非静态方法,不能通过类名调用//D.say();//报错!!!需要先创建对象,再调用new D().say();//可以}}class D {private int n1 = 100;private static int n2 = 200;public void say() {//非静态方法,普通方法}public static void hi() {//静态方法,类方法//类方法中不允许使用和对象有关的关键字,比如this和super。//而普通方法(成员方法)可以使用。//System.out.println(this.n1);//报错!!!}//类方法(静态方法)中 只能访问 静态变量 或静态方法//口诀:静态方法只能访问静态成员.public static void hello() {System.out.println(n2);System.out.println(D.n2);//System.out.println(this.n2);//报错!!!hi();//OK//say();//报错!!!}//普通成员方法,既可以访问 非静态成员,也可以访问静态成员//小结: 非静态方法可以访问 静态成员和非静态成员public void ok() {//非静态成员System.out.println(n1);say();//静态成员System.out.println(n2);hello();}}
