In C ++, this is the case.
Http://blog.csdn.net/shandianling/article/details/8136003
In Class Definition
1. In the class, only static variables are declared and not defined. // Common variables are also declared and then defined when the object is instantiated
2. The declaration only indicates the data type and attribute of the variable and does not allocate memory. The definition requires memory allocation.
NOTE: If int A is written in the class, both variables are declared and variables are defined. The two are combined.
3. Static members are "Class-level", that is, they are equivalent to classes, while common members are "Object (Instance) level. A class-level member exists before any object of the class and is shared by all objects of the class.
4. Now, let's assume that we want to instantiate an object of this class, what will happen? Static members must appear in this object, right? Will that static member be defined at this time? This is obviously not suitable. For example, if another thread needs to create an object of this class, define the static member according to this method. There are two possible scenarios: 1. Repeated definition; 2. Competition will occur even if no repeated definition is generated, resulting in a deadlock problem, so that the object cannot be created. Obviously, the compiler cannot do this. A reasonable solution is to define it outside the class and share it with all objects.
Note: static constant members of a class can be initialized in the class.
But of course it is not in Java.
In Java class definitions, both static member variables and generic non-static member variables can assign initial values.
Http://book.51cto.com/art/201207/350808.htm
The following declaration provides an initial value for the static member PI and count in the sphere class:
Class sphere {
Static final double Pi = 3.14; // class variable that has a fixed value
Static int COUNT = 0; // class variable to count objects
// Rest of the class...
}
You can also initialize common non-static data members in the same way. For example:
- Class sphere {
- Static final double Pi = 3.14; // class variable that has a fixed value
- Static int COUNT = 0; // class variable to count objects
- // Instance variables
- Double Radius = 5.0; // radius of a sphere
- Double X center = 10.0; // 3D coordinates
- Double ycenter = 10.0; // of the center
- Double zcenter = 10.0; // Of A Sphere
- // Rest of the class...
- }
Now, each object in the sphere class takes 5.0 as the radius and the center coordinates of the ball as the point (10.0, 10.0, 10.0) at the beginning ).
Some content cannot be initialized using a single expression. For example, if you want to initialize a large array as a data member, you can use an initialization code block because multiple values need to be calculated.
Use initialization code block
The initialization code block refers to a piece of code located between two curly braces, which are executed before the class object is created. There are two types of initialization code blocks:
The static initialization code block uses a code block defined by the keyword static, and is executed once when the class is loaded. Static initialization code blocks can only initialize static data members of a class.
The non-static initialization code block is executed for each created object, so instance variables in the class can be initialized.