ios-static methods (class methods) and Instance methods

Source: Internet
Author: User

1. Example method/dynamic method

a). Identifier:-

b). Invocation method: (Instance object function)

c). The instance method is on the stack.

2. Static method/class method

a). Identifier: +

b). Call Mode: (class function)

c). Static methods allocate memory on the heap.

3. The distinction between static and instance methods

A). Static method resident memory, 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.

b). 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, which is not writable.

c). Instance methods need to create an instance before they can be called, it is cumbersome, static methods are not used, relatively simple.

d). Static methods are statically bound to subclasses, not inherited.

e). Generally use frequent methods with static methods, using less methods with dynamic. Static speed is fast, which accounts for memory. Dynamic speed is relatively slow, but after the call, the immediate release of the class, you can save memory, you can choose according to your own needs are dynamic or static methods.

f). Static methods modify the state of the class, and the object modifies the state of the individual objects.

g). An instance invocation of a class exists in the life cycle of the class, and when the class is gone, the corresponding instance is gone, and the corresponding method is gone. Static class otherwise, as long as you reference the namespace of that static class, it will persist until we launch the system.

Note: The following is the use of the class method, features: First letter capitalization, instance method is often the first letter lowercase

ios-static methods (class methods) and Instance 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.