静态static

Posted xzwx668

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了静态static相关的知识,希望对你有一定的参考价值。

概述:一旦用了static关键字成员内容就不属于该对象而是属于本类且所有本类对象共享一份

public class Static2 {
    static String room;
    static {
        System.out.println("静态代码块执行");
    }
    public Static2(){
        System.out.println("构造方法执行");
    }

}
public class Myclass {
    int num;
    static int num1;

    public void method() {
        System.out.println("这是一个成员方法");
    }

    public static void methodStatic() {
        System.out.println("这是一个静态方法");
        /*System.out.println(num);*///静态不能直接访问非静态,
        /*System.out.println(this);*///静态方法中不能使用this
    }
}

以上是关于静态static的主要内容,如果未能解决你的问题,请参考以下文章