Java中static的理解,第1张

Java中static的理解,第2张

静态变量:类变量,为此类所有对象共享
  静态方法:静态方法里没有this引用
  Examda提示: 不能在静态方法中访问非静态的成员变量和方法
  可以直接通过类访问静态成员,即使不存在该类的对象
  //Student.java
  package cn.edu.uibe.oop;
  public class Student {
  String name; //学生姓名
  static int counter=0; //学生对象的数目
  public Student(String name){
  this.name = name;
  counter++; //对象计数加1,需要用静态变量才能为所有对象共享
  }
  public void print(){
  System.out.println("name="+name+"\tcounter="+counter);
  }
  public static void showCounter(){
  System.out.println("counter="+counter);
  //System.out.println(name); //error,静态方法里面不能访问非静态的成员变量和方法
  //this. //error,静态方法里面没有this引用
  }
  public static void main(String[] args) {
  Student[] student = {
  new Student("zhangsan"),
  new Student("lisi"),
  new Student("zhaowu")
  };
  for(int i=0;i

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » Java中static的理解

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情