Order of Instruction
Background
Static fields
static functions
Static methods
Questions and Answers
Background
When writing a class, it is sometimes necessary for a member of a class to be unique, that is, to keep only one state for all objects. such as Create * * *, we are all Chinese, but can not everyone to save a Chinese field bar, with one is enough .
MSDN says: Using the static modifier declares a static member that belongs to the type itself rather than to a particular object. In other words, the object modified with static is all members of a class and is not unique to a class. For example, a sedan has four wheels, not a car with four wheels.
2. Access does not require declaring class objects. Like what
Console.Write ();//write () This is a static method that does not require the new Consolo ();
A 2.static object is unique, regardless of whether it produces an object or, in the case of how many objects are produced, there is only one copy of certain data in memory space. In other words, you have to change the number of static variables, no matter how many members you have in the new class.
Static fields
The so-called static field, that is, the field of static modification (yes, that is nonsense O (∩_∩) o650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0028.gif "alt=" j_ 0028.gif "/>).
No matter how many objects are created, static fields represent only one of the data in memory space. For example, the system time, no matter how many software you installed, you want to know what time it is, there is only one reference value, if you change the time, then the other software acquisition time has changed. (Of course, just for example, specifically, I don't care about the time of visit).
A static field is the first object that is instantiated when a class operation (instantiation, calling a static method, and so on) is initialized. The second is a static constructor, and both of them can only be called once.
Because the static member itself is for global uniqueness, of course, static members within the entire function equivalent to global variables.
A static member can only be initialized once, and is created the first time the class is manipulated. When other operations are performed on the entire class, it is no longer created because the static members are already there. (That is, to the rookie of the nonsense, or many people will have doubts, such as me.) )
650) this.width=650; "style=" width:732px;height:433px; "src=" http://s3.51cto.com/wyfs02/M00/6B/AA/wKiom1Uz_ Yagzwtfaajvdjnoqe0665.jpg "title=" Untitled. png "alt=" wkiom1uz_yagzwtfaajvdjnoqe0665.jpg "height=" 433 "hspace=" 1 "border= "1" vspace= "1" width= "732"/>
For example, each time an instance is created, the constructor makes +1 for two fields, but the result is different, which is the uniqueness of the static decorated field.
Static constructors
Static constructors are typically used to initialize static variables and do not initialize non-static variables, but because non-static constructors can also manipulate static variables, it is less useful to use static constructors.
Regardless of what you do with a class, a static constructor is always the first method to be manipulated except for a static field, which is more advanced than a non-static parameter, a non-static constructor.
Only static members can be manipulated, because non-static members require an entity reference.
The entire lifecycle is run only once and cannot be called.
A static constructor cannot have modifiers, because only the system can be called, and there is no public that is useless; it cannot have input parameters.
If the class contains the main method, the static constructor is executed first;
class A { Public static int x; static a () { x = b.y + 1; } } class B { public static int y = a.x +1; Static b () { } static void main (String[] args) { console.writeline (StriNg. Format ("Y={0}, x={1}", b.y, a.x));//y=2, x=1; console.readline (); } }
This is a very famous online example of static, if you do not look at the answer, can be counted out is the ability.
First, the Main () method is inside Class B, so you need to call Class B first.
Enter Class B, assign space to the static field y first, and assign a value of 0 by default; Notice that Y is initialized here.
Because y=a.x+1, the static field of Class A is now entered in Class A.
In the same way, a class X is assigned a space and assigned a value of 0. Then x=b.y+1,b.y already exists, for 0, so x=b.y+1 after execution, X = 1.
Call the static constructor of a.
Complete the assignment of Y, y=a.x + 1,y=2.
Call the static constructor of B.
The main () method is called, because A.X,B.Y is initialized and assigned a value, so write the answer directly.
Be able to describe the complete sequence, static general interview questions are hard for you (I mean I am very cow 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0015.gif "alt=" J _0015.gif "/>-Bye"
Static methods
Static methods are generally used for frequent calls and infrequently modified places. such as the basic statement to connect to the database. Of course, this feature is also due to the fact that static methods cannot be inherited and cannot be extended.
A static method call does not require instantiating a class.
Static methods can only reference static methods, fields, properties. Static methods cannot have non-static variables.
Once a static method is called, it cannot be destroyed unless the entire application domain is finished. So the static function cannot be too much, otherwise it always occupies memory.
Because static methods cannot access non-static variables, they are less coupled with non-static methods, which facilitates code modification.
Static methods are more efficient than non-static methods, but cannot be used frequently because they do not always consume memory and cannot be destroyed.
Questions and Answers
What is the biggest difference between a static variable and a non-static variable?
The biggest difference is in the location of the memory. the memory of a static variable takes up memory when the program starts to hold it, and the variable frees the memory until the end of the program. A non-static variable allocates memory when the program runs to that step and automatically destroys it when it finishes executing. Therefore: The value of the static variable is initialized only once, and each subsequent access is the last processed value.
Why static variables do not need to be instantiated to be accessible.
Since classes are initialized, they are allocated memory space for static variables, they can be found exactly where they are without instantiation, and non-static members must instantiate the class object before they can determine their memory location;
So there is also the argument that a static member belongs to a class and not a static member belongs to the object of the class
Why can't I access non-static variables inside a static method, or create non-static variables?
Because a non-static variable needs to be instantiated to confirm its location in memory, the static function does not need to be instantiated, so the static method cannot use the
Why can't I declare a static variable inside a method?
Because a static variable is a common variable for all instances of a class, it is a global variable within the scope of the class. If you define a static variable inside a method, it causes the static variable that is defined to be a local variable, and so the definition naturally goes wrong.
Declaration period for Static members
The life cycle of a static member is the lifetime of the entire application domain. That is, start with the class that contains the static member, until the end of the entire application.
The above information is my reference to a myriad of information to organize themselves, of course, there may be some errors, welcome to correct the aunt, I will try to revise, so that this article as a good static reference .
This article is from the "Code Creep Growth" blog, be sure to keep this source http://zbqldyj.blog.51cto.com/8026445/1636305
C # static Dry Goods Full analysis