A detailed summary of static and non-static

Source: Internet
Author: User

It took a bit of effort to figure it out:

You'll see that there are places that are constantly recurring--that's what you're looking for.

This article refers to a number of places, the specific source is not written.

Static and non-static:

A static member of a field, a function member's declaration, or a non-static member (instance member) when it contains the static modifier;

Data members can be divided into static variables, non-static variables, two kinds. Static members belong to the class, not static members (instance members) that belong to the object. For a data member of a class, if it is static, then he will be part of the class, shared for all instances, and if non-static, each instance has a copy of the Test bay.


A non-static member of a class belongs to an instance of the class , and each instance that creates a class opens up an area in memory for a non-static member, whereas a static member of a class belongs to the class and is shared by all instances of the class, regardless of how many copies the class creates. A static member occupies only one area of memory.


If a static member is accessed in E.M form, E must be a class; Access instance members, E must be instances.
A static field of a class is assigned only one storage unit, and each instance field of the class has a separate copy.
You cannot access instance members in a static function member, you cannot use this, instance members and static members can be accessed in instance function members, and this can be used.

Static member : A member in a static class joins the static modifier, which is a statically member . You can access this static member directly by using the class name + static member name , because static members exist in memory , non-static members an instantiation is required to allocate memory , so a static member cannot access a non-static member: Because static members exist in memory, non-static members can access static members of the class directly.


non-static member: All members that do not have static members are nonstatic, and when the class is instantiated , it can be accessed by instantiating the class name . The lifetime of a non-static member is determined by the lifetime of the class . Static members, however, do not have the concept of a lifetime because static members always reside in the content .


syntactically speaking, static functions do not have this pointer. Because there is no this pointer, it cannot be referenced by an object, but only by the class name.
Non-static member variables cannot be referenced inside a static function. When calling a normal function, the this pointer of the class instance is implicitly passed to the function as the first argument. From this point, the normal function is dependent on the instance of the class, and no class instance can be called. The static function does not have this implicit argument. So call it without relying on the class instance. That is, it can be called by Classname::funname. The benefit of a class static function for a global function is that it can use the private and protected members of the class (when he obtains a pointer to the class instance, such as a parameter passed in).

In terms of abstract semantics, the actions of static functions belong to the whole class, not to a specific object.


Static and non-static are mainly the following differences:
1. From a storage point of view, static variables and methods allocate memory in the static store, rather than statically allocating memory on the stack or heap
2. From the scope, static variables exist within the scope of the entire program, while the non-static variables have a local action time.
3. From the standpoint of use, static variables and methods do not have this pointer and can only be referenced by the class name.
4. For a data member of a class, if it is static, then he will be part of the class, shared for all instances, and if non-static, each instance has a copy of the Test bay.


You can also include static and non-static members in a class, as well as static constructors and non-static constructors.

For a data member of a class, if it is static, then he will be part of the class, shared for all instances, and if non-static, each instance has a copy of the Test bay.

Static member functions have only one copy, whereas ordinary member functions have one copy in each class object.

The

constructor is a function member that initializes a class or instance. A
destructor is a function member that is called automatically when an instance of a class is revoked.
The constructor and destructor cannot be inherited, and all other members can be inherited.

Static constructor: Implements a function member that initializes a class.

Instance constructor: A function member that is used to initialize an instance of a class when an instance is created.
 
  for WinApp, static members are a lot of things for programmers themselves, and because static members reside in memory and pass shared data between methods and methods, static members are my first choice. But do not use it for convenience, especially when memory is tight or
  use static methods to manipulate some shared values. Or to write a multiuser system, be cautious. For example:
 static int id = 0;
 sql = "SELECT * FROM table where id=" + ID;
 
  If this is the case, there is no problem with the single-machine test, but there is a problem when multiple people test the data at the same time. If a user accesses his ID is 20, then the value of the ID is 20 in memory, and at this point the B user accesses it, His ID is 30, then the ID value in memory is 30. The ID value of a user has been changed. If you write this method with a non-static member at this point, this does not happen: Because a non-static member is the time you declare, the memory is allocated when it is instantiated. So when a user accesses, the app allocates memory for a user's request because of the instantiation of a. While the B user accesses the same time because the B user's access to allocate memory. So two users are accessing different blocks of memory. So there will be no data coverage and confusion phenomenon ...
 
I think this situation should be a good illustration of the difference between static and non-static members.
  
In contrast to WebApp, the use of static under WinApp is much less than the factors considered under WebApp, because WebApp is a multi-user system, so use static should be more careful:

And I have a question about the use of static under WebApp, if a static method, for example:

static string AA (String str) {
After a series of operations:
return str;
}

Or a static method that returns a dataset
Static DataSet AA (String str) {
After a series of operations:
return DataSet;
}

At this time, when the traffic is large, the program has been concurrency, will there be confusion?? My previous project used a lot of static methods in the common function class, but fortunately, there was no problem with the amount of traffic. Before I sent this article, I looked up msdn,csdn and searched for some articles about static members, but none of them had a clear explanation. Although, I have been testing the project for such a long time also no problem. But there's always a chance that this could happen.

Do not know whether people encounter similar doubts in the project?? Please advise a friend who has had experience in this area.


Answer:
Do not say whether or not to misuse, if you appear to conflict, you do not understand the static member variable and static method of the difference, the static method itself is only a piece of code, no matter how to call him no problem. But static member variables are not, he is shared by all users, if a user changed him, it will certainly affect others, this is often said concurrency conflict problem, in general, when modifying shared member variables to lock!


some misunderstandings about static methods and instance methods.

static method resident memory, the instance method is not, so the static method is high efficiency but occupies memory.  In fact, the method is the same, the static method and the instance method are the same when the time is loaded and the memory is occupied, which is loaded when the type is used for the first time. The speed of the call is basically no different.   The static method allocates memory on the heap, and the instance method is on the stack.  In fact, none of the methods can allocate memory on the heap or on the stack, as the code is loaded into a special area of code memory that is not writable.   third, the instance method needs to create the instance first to be able to call, compares the trouble, the static method does not have, relatively simple. in fact, if a method has nothing to do with an instance of his type, it should be static and never be written as an instance method. So all the example methods are related to the instance, since it is related to the instance, then creating an instance is the inevitable step, without the hassle of simply saying it. You can actually write all of the instance methods as static, passing in the instance as a parameter. Some methods appear to be unrelated to the instance in which they are located, such as the Icomparer.compare method, but virtually every class that implements this interface will only be responsible for the comparison of their own type instances, which is a legacy problem that is not present in the c#1.x specification. most static methods are related to instances of classes, such as the various parse methods, which he makes static because he has no instances as arguments. Most of the others are for semantic or other purposes.  Here's a simple example .Code

A detailed summary of static and non-static

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.