==static on properties: all obj shared the same variable which is on the globe data area . one obj change it, all objt changes
class Person
{
private int age;
static String city = "Beijing";
public Person(int age) {
this.age = age;
}
public String getInfo() {
return "age=" + this.age + ",city=" + this.city;
}
}
public class testb
{
public static void main ( String args[] ) {
Person per1 = new Person(30);
Person per2 = new Person(20);
per1.city = "Shanghai";
Person.city = "Shanghai";
System.out.println(per1.getInfo()); //==> shanghai
System.out.println(per2.getInfo()); //==> shanghai
}
}
===the static properties and method can called from the class directly
===the static method can only use static properties and static mehtod;
===the static method can be called without any objects
===public static void main (String args[]) must exactly
main is reserved.
===memeoty
1. stack: reference to obj
2. heap: properties of objs
3. global code: method of class
4. global data: static properties of class
没有评论:
发表评论