Blind Zone generated when initializing static members and initialized static blind zone

Source: Internet
Author: User

Blind Zone generated when initializing static members and initialized static blind zone

Static members belong to Classes rather than instances. Static members have a wide range of applications. For example, public static member values can be obtained or set globally in an application, which is similar to the global variables of C ++, however, improper use may also cause problems.

Class Test
{
Private static string strStaticName = "Hello ";
// No parameter Constructor
Public Test ()
{
}
// Parameter Constructor
Public Test (string strName)
{
StrStaticName = strName;
}

Public string GetName ()
{
Return strStaticName;
}

}

Static void Main (string [] args)
{
Console. WriteLine ("when you create a t1 instance, set the name to Oriental ");
// Create instance t1
Test t1 = new Test ("oriental ");
Console. WriteLine ("so the name obtained using the GetName method is: {0}", t1.GetName ());
Console. WriteLine ("");

// Create instance t2
Console. WriteLine ("when creating t2 instance, the name is not set ");
Test t2 = new Test ();
String strName = t2.GetName ();
Console. WriteLine ("but the name obtained through the GetName method is: {0}", strName );
Console. Read ();

}

After debugging:

Analysis: in an application, no matter how many instances of a class are created, the static fields of the class are initialized only once.

Solution: 1) Cancel static keyword static

2) All constructors with parameters are used.

3) assign values to static fields in the no-argument Constructor

Public Test ()

{

StrStaticName = "Hello ";

}


C ++ static member Initialization

Yes. The system will not automatically initialize.
Because, during the declaration, no actual object is generated when the class is defined, and the static member must exist, initialize the class outside and tell the compiler that it is ready to generate the object!

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

1. In the class, only static variables are declared and not defined. 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.

Related Article

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.