Static and non-static

Source: Internet
Author: User

1. Static and non-static classes
●. Static classes can only contain static members and static methods; otherwise, a compilation error is thrown;
Non-static classes can contain non-static members and non-static methods.
Static member and static method;
● Static classes cannot be instantiated. Non-static classes can be instantiated. Static or
Non-static class. Calls to static members and static methods must be implemented through classes;
● If a class only contains static members and static methods, it should be marked as static,
It also provides private constructor to avoid instance creation. In fact, this is also the embodiment of single-piece simulation;

2. Static constructor and instance Constructor
Static constructor. Static members in the user initialization class, including static fields and static attributes.
A static constructor cannot be a parameter, an access modifier, or a call.
The Runtime Library is executed before the class member is called for the first time.
●. Static constructor, which can be stored in the same way as an instance constructor without parameters. Although the parameter list is the same,
However, the execution time is different between the two. The static constructor is executed when the Runtime Library loads the class. The instance Constructor
The function is executed when the instance is created.
●. A static constructor can only initialize static members, but cannot use non-static members;
The instance constructor can initialize instance members or static members, but static
Except for read-only fields;
● The static constructor is executed only once, and the. NET Runtime Library cannot determine what the static constructor is
And the instance constructor can be executed multiple times when an instance is created;
● A class can have only one static constructor, and a class can have multiple instance constructor;
● Static members can be initialized during declaration or through static constructor,
Both types of initialization can only be executed once. In general, simple static members are declared at the beginning
The initialization is fine, while the complex static members should be better initialized in the static constructor;
●. General execution sequence of the constructor: allocating the memory space of static members ---- initial execution of static members
Start-Execute static constructor-allocate the memory space of the object instance-Execute instance members
Initialization ---- execute the instance constructor;

3. Static and instance members
●. Static members include static fields and static attributes. static fields are implemented as private, whereas static attributes
It is generally implemented as public to reflect the encapsulation principles of classes;
●. Static members and classes are associated and exist independently of objects. They can only be accessed by classes. instance members and
Objects are associated and can only be accessed by object instances, but not by classes;
●. Static members belong to the class. No matter how many instance objects are created, the static members have only one copy in the memory;
The instance members belong to all instances of the class. Each time an instance object is created, the instance members are allocated in the memory.
One memory area;

4. Static methods and instance methods
●. Performance, both of which are similar;
●. Static methods can only access static members and static methods, and can be accessed indirectly by creating instance objects
Ask the instance Member and instance method. The instance method can directly access the instance Member and instance method, or
Direct access to static members and static methods;
●. Static methods can only be accessed by classes, and instance methods can only be accessed by object instances;
● The keyword this cannot be referenced in static methods, but the instance method can;
● Static methods cannot be marked as virtual, abstract, or override. Static methods can be derived.
Class access, but cannot be overwritten by the derived class.
● The. Main method is static, so the main method cannot directly access the instance methods and instances of the main class.
Member.
●. The static state must be thread-safe.

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.