标签:事物 成员 nbsp print his 拷贝 private 代码 静态方法
静态方法是否能调用非静态成员变量?
    static关键字具有如下特点:
        一、static关键字修饰的属性/方法可以通过类名直接调用,而不必先new一个对象。
        二、static关键字修饰的属性只在内存中有一份拷贝,无论创建多少个对象,该类的static属性在内存中只有一份,被所有对象公用。
    
    也就是说,当声明一个事物是static的时候,就意味着这个域或这个方法不会与包含它的那个类的任何对象实例关联在一起。
用代码进行测试:
Class MyStatic{
    public static int num = 10;
    private int age;
    MyStatic(intage){
        this.age=age;
    }
    public static void fun(){
        System.out.println(num);
        System.out.println(age);       //报错,非静态属性age不能在静态域中被引用
    }
}
我们发现,static修饰的方法fun()不能调用MyStatic类中的非static属性age。
标签:事物 成员 nbsp print his 拷贝 private 代码 静态方法
原文地址:https://www.cnblogs.com/cyx-qk/p/10989081.html