The difference between static and non-static members

Source: Internet
Author: User
Tags instance method

Static and Non-static in the online collection of a lot of information, summed up the next, if the little brother there mistakes and wrong hope that you big brother pointing.

Data members can be divided into static and non-static variables.
Static members: Members in a static class are added to the static modifier, which is a statically member. You can access this static member directly using the class name + static member name, because static members exist in memory, and non-static members need to be instantiated to allocate memory, so static members cannot access non-static members. Because static members exist in memory, non-static members can directly access static members of a class.

Non-static: all members that are not static are non-static members and can be accessed through the instantiated class name after the class has been instantiated. The lifetime of a non-static member depends on the lifetime of the class. Static members do not have the concept of a lifetime because static members always reside in the content ...

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

For WinApp, static members save a lot of things for the programmer itself, and because static members reside in memory and pass shared data between methods and methods, static members are my first choice. But don't use it for convenience, especially in memory tension or
When you use a static method to manipulate some shared values. Or to write a multi-user 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 stand-alone test, but when multiple people are testing the data at the same time, it's going to be a problem. If, a user accesses his ID is 20, then the ID's value in memory is 20, but at this time the B user accesses, his ID is 30, then the ID in memory value is 30. A user's ID value has been changed ... If you write this method in a Non-static member at this point, this will not happen. Because non-static members are declared, the memory is allocated when instantiated. So when a user accesses, the app allocates memory for a user's request because a is instantiated. and b user access also will be due to the access of B users to allocate memory. So two users are accessing different chunks of memory. So there is no data coverage and confusion ...

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 under WebApp considerations, because WebApp is a multi-user system, so use static time should be more careful.

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

static string AA (String str) {
After a series of actions ...
return str;
}

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

This time, in a large number of visits, the program appeared concurrency, will not occur disorder?? My previous project used a large number of static methods in the common function class, but fortunately there was no problem with the amount of traffic. Before sending this article, I looked up msdn,csdn and searched for some articles about static members, but there was no clear explanation. Even though I've been testing it for so long in my project, there's no problem. But it's always felt like this could happen.

Do not know if you have encountered similar doubts in the project?? Please advise the friends who have experience in this field.
Answer:
Do not say whether abuse, if you conflict, that you do not understand static member variables and static methods, the static method itself is only a piece of code, no matter how the call he will not have problems. But the static member variable is not, he is shared by all users, if a user changed him, will certainly affect others, this is often said concurrency conflict problem, in general, when you modify the shared member variable to lock!


Some misconceptions about static methods and instance methods.

One,     static method resident memory, the instance method is not, so the static method is efficient but takes up memory.       In fact, the methods are the same, both in loading time and in memory, static methods and instance methods are the same, loaded when the type is used for the first time. The speed of the call is basically no different.     II,     static method allocates memory on the heap, instance method on the stack.       In fact, none of the methods can allocate memory on the heap or stack, as the code is loaded into a particular area of code memory, which is not writable.     III,     instance methods need to create an instance before it can be invoked, more cumbersome, static methods do not, relatively simple.     in fact, if a method has nothing to do with an instance of his type, then it should be static and never be written as an instance method. So all of the instance methods are related to the instance, and since the instance is related, creating the instance is a necessary step, no trouble, simply say. In fact, you can write all the instance methods as static and pass the instance as arguments.     Some methods seem 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 of the history of the c#1.x specification without generics.     most static methods are related to instances of classes, such as various parse methods, and the reason he is static is that he has no instances as arguments. Most of the others are considered for semantic or other purposes.  

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.