The difference between static and non-static classes and static and non-static methods

Source: Internet
Author: User

1, static classes can not include non-static members, such as non-static methods, constructors, non-static classes may include static members. If you do not follow this rule, the static class compiler will check for an error.

In short, all members in a static class must be static members, but static members are not necessarily in static classes.

2. Static classes cannot be instantiated, their members can be used directly, static members in non-static classes do not need to be instantiated, they can be used directly, and non-static members in non-static classes must be instantiated.

In a nutshell, a static member does not need to instantiate its class, not a static member must instantiate the class it resides in.

3, static member execution speed is fast, because it is compiled at the time of the compiler placed in the static zone (saving automatic global variables and static variables) of the contents of the rest of the total program life, and the ordinary class of non-static members need to instantiate, in the stack to save the reference address, the heap to save the instance object, To get the method, so the speed is slow.

Static member variables are loaded into the static area of the memory when the program starts, regardless of whether the methods or properties are used later
。 Even if no one is accessing the program, this part of the memory is still not released.

4. Static member variables are shared, which means that when an instance of a class modifies the static member variable, its modified value is the other
See all the examples.

5. Static methods and properties cannot access non-static fields and events in their containing types.

6. Static methods can only be overloaded, not overridden, because static methods do not belong to instance members of the class.

7. C # does not support static local variables (static variables are defined inside a method).

8, static class is sealed class (sealed), cannot be inherited. Its sealing is the function of self-protection, and conforms to the packaging idea of the three main ideas of programming.

9, the static member is equal to the global variable, the whole system exists in a unified area (static zone), is shared, such as static into Num=0, when the compilation of Num is already 1, a thread changed its value for 1,b thread to get the time num is 1.

The difference between static and non-static classes and static and non-static methods

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.