用static声明的变量为静态成员变量,通过引用或类名访问静态成员,不需要实例化。
class="java" name="code">//staic关键字 //非静态的成员变量不能在静态的上下文中使用 public class Cat { private static int sid = 0; private String name; int id; Cat(String name) { this.name = name; id = sid++; } public void info(){ System.out.println("My name is "+name+" No."+id); } public static void main(String arg[]){ Cat.sid = 100; //sid = 100 Cat mimi = new Cat("mimi"); mimi.sid = 2000; Cat pipi = new Cat("pipi"); mimi.info(); pipi.info(); } }
?