# V3070. Uninitialized variables are used when initializing the 'A' variable.

The analyzer detected a possible error that has to do with initializing a class member to a value different from the one the programmer expected.

Consider the following example:

    class AClass {
      static int A = B + 1;
      static int B = 10;
    }

In this code, the 'A' field will be initialized to the value '1', not '11', as the programmer may have expected. The reason is that the 'B' field will be referring to '0' when the 'A' field will be initialized. It has to do with the fact that all the members of a type (class or structure) are initialized to default values at first ('0' for numeric types, 'false' for the Boolean type, and 'null' for reference types). And only then will they be initialized to the values defined by the programmer. To solve this issue, we need to change the order in which the fields are processed:

    class AClass {
      static int B = 10;
      static int A = B + 1;
    }

This way, the 'B' field will be referring to the value '10' when the 'A' field will be initialized, as intended.

This diagnostic is classified as:

CWE-457

You can look at examples of errors detected by the V3070 diagnostic.