- In the class, with static member variables declared as static member variables, it is a public variable of the class, are initialized on first use, for all objects of that class who, only a static member variables .
- Static method declared as static method, when the method is called, does not reference the object passed to it, so in a static method can not access non-static members .
- Is no longer a static method call on an object, it can not access non-static member
- By reference to the object or class name (do not need to instantiate) access static members.
public class Cat { private static int sid = 0; private String name; private 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[] args) { Cat.sid = 100; Cat mimi = new Cat("mimi"); Cat pipi = new Cat("pipi"); mimi.info(); pipi.info(); } }
operation result:
My name is mimi No.100
My name is pipi No.101
Memory analysis: