Note the static keyword in java

1, the position stored in the memory: (static methods and properties of the modified area saved in the method, but the method area is a part of the stack)

     Memory partition

 

2. What kind of property can be defined as static data

E.g:

class person{

public String name;

pulic String guoji;

public int age;

punblic person(Stirng name,String guoji,int age){

this.name=name;

this.guoji = guoji;

this.age=age;

}

}

If the above classes, guoji for more than a property is the same in terms of objects (for example, values ​​are: China), so this time it should guoji property is set to static

E.g:

public static String guoji = "Chinese";

The purpose of this is done to save memory overhead, but the drawback is, but after a certain strength of the international object re-assignment, for example, the value "US", then the nationality of all instances of an object becomes a "United States of America."

3, how to call an external method static method

First, the internal static methods can not call non-static method directly, because the static method does not belong to the object, but belong to the class, he is loaded with class and load, rather than the static method you need to call back by instantiating objects All, while if you want a non-static method call inside the static method, you must first instantiate an object, then the object-point method can.

 

 

 

Guess you like

Origin www.cnblogs.com/jiazhutao/p/11959471.html