对于Static的思考

2017-12-07  本文已影响0人  QinRenMin

作用:用于修饰成员(成员变量和成员函数)

被修饰后的成员特点:
注意:

例如:

class Person{
  String name; //实例变量(成员变量)存在堆中
  static String country = "CN";//静态变量(类变量)共享数据,节约内存
public void show(){
  System.out.println(country+":"+name);//前者省略类名,后者省略this
}
}
class StaticDemo{
  public static void main(String[] args){
    Person P = new Person();//P是类类型变量
    P.name = "Jane";
   P.show();
    System,out,println(Person.country);
    System.out.println(P.country);
}
}

什么时候用静态?(没有访问特有数据)

上一篇 下一篇

猜你喜欢

热点阅读