Running effect:
Console effect:
========================================================== ============
Code Section
========================================================== ============
/Hello_test/src/com/b510/test/StaticTest. java
Copy codeThe Code is as follows :/**
*
*/
Package com. b510.test;
/**
* Differences during program running: instance variables belong to the attributes of an object, and an instance object must be created. <br>
* The instance variable is allocated space before the instance variable can be used. Static variables do not belong to a certain <br>
* Instance objects, but belong to the class, so it is also called a class variable, as long as the program loads the class bytecode, <br>
* Without creating any instance object, static variables are allocated space and can be used. <Br>
* In short, instance variables can be used only after an object is created. Static variables can be used. <br>
* Directly use the class name for reference. For example, no matter how many instance objects are created for the following program, <br>
* Only One <code> staticInt </code> variable is always assigned, and each time an instance object is created, <br>
* This <code> staticInt </code> adds 1. However, each time an instance object is created, a <code> random </code>, <br>
* Multiple <code> random </code> may be allocated, and each <code> random </code> value is added only once. <Br>
*
* @ Author <a href = "mailto: hongtenzone@foxmail.com"> 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 ();
}
}