Operation effect:
Console effect:
==================================================
code part
==================================================
/hello_test/src/com/b510/test/StaticTest.java
/**
* The difference when the program is running: instance variables belong to the properties of an object, and an instance object must be created,<br>
* Only the instance variable in it will be allocated space and this instance variable can be used. Static variable does not belong to a<br>
* An instance object, but belongs to a class, so it is also called a class variable. As long as the program loads the bytecode of the class,<br>
* Without creating any instance objects, static variables will be allocated space and static variables can be used. <br>
* In short, instance variables must create an object before they can be used through this object, while static variables can<br>
* Directly use the class name to reference. For example, for the following program, no matter how many instance objects are created,<br>
* Only one <code>staticInt</code> variable is always allocated, and every time an instance object is created, <br>
* This <code>staticInt</code> will be incremented by 1; however, every time an instance object is created, a <code>random</code> will be allocated,<br>
* That is, multiple <code>random</code> may be allocated, and the value of each <code>random</code> is only incremented once. <br>
*
* @author <a href="mailto:[email protected]">hongten</a>
* @date 2013-3-2
*/
public class StaticTest {
private static int staticInt = 2;
private int random = 2;
public StaticTest() {
staticInt++;
random++;
System.out.println("staticInt = "+staticInt+" random = "+random);
}
public static void main(String[] args) {
StaticTest test = new StaticTest();
StaticTest test2 = new StaticTest();
}
}