/*
member variable and static variable:
1,invoke ways:
member variable,also called 'instance' variable,which can only be invoked by instance.
static variable,also called 'class' variable,which [can also] invoked by class,and it is recommended that it invoked using 'class' rather than 'instance'.
2,storage places:
instance variable is stored in the 'heap',
whereas, static variable is stored in the static area of the 'method area'.
3,life cycle:
static var exists as the existence of the class, and gone as the disappear of the class.[general] the 'class' exists when the JVM works,and disappear when the JVM stopped.
instance var exists as the existence of the instance,and gone when the instance was released.
4,memory location:
static var located in the static area of the Method Area,whereas,
instance var located in the heap.
*/
package kju.obj;
import static kju.print.Printer.*;
public class InstanceVarStaticVar {
public static void main(String[] args) {
println(Person.country); //use the 'static' var by 'class' instead of the instance.
printHr();
Person lily = new Person("lily");
println(lily.getName());
println("set:");
lily.setName("lucy");
println(lily.getName());
println();
}
}
class Person {
public static final String country = "cn";
public Person(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
private String name;
}