Why do static member variables of a class need to be initialized outside the class?

Source: Internet
Author: User

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:

  1. Class sphere {
  2. Static final double Pi = 3.14; // class variable that has a fixed value
  3. Static int COUNT = 0; // class variable to count objects
  4. // Instance variables
  5. Double Radius = 5.0; // radius of a sphere
  6. Double X center = 10.0; // 3D coordinates
  7. Double ycenter = 10.0; // of the center
  8. Double zcenter = 10.0; // Of A Sphere
  9. // Rest of the class...
  10. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.