Instance method/class method, instance method

Source: Internet
Author: User

Instance method/class method, instance method

1. instance method/Dynamic Method

A). identifier :-

B). Call method: (Instance Object Function)

C). The instance method is on the stack.

2. Static methods/class methods

A). identifier: +

B). Call method: (Class Functions)

C) The static method allocates memory on the heap.

3. Distinction between static methods and instance methods

A) The static method is resident memory, and the instance method is not. Therefore, the static method is highly efficient but occupies the memory. In fact, the methods are the same. In terms of loading time and memory usage, the static method is the same as the instance method, and is loaded when the type is used for the first time. There is basically no difference in the call speed.

B) The static method allocates memory on the stack, and the instance method is on the stack. In fact, it is impossible for all methods to allocate memory on the heap or stack. As code, methods are loaded into special code memory areas, which cannot be written.

C) The instance method can be called only after an instance is created. It is troublesome and the static method is not needed. It is relatively simple.

D) The static method is statically bound to the subclass, not inherited.

E). Generally, static methods are used frequently, and dynamic methods are used in a few ways. Static speed is fast, occupying memory. The dynamic speed is relatively slow, but the class is released immediately after the call, which can save memory. You can choose whether to use the dynamic method or static method as needed.

F) The static method modifies the class state, while the object modifies the state of each object.

G). The instance call of the class exists in the life cycle of the class. When the class does not exist, the corresponding instance does not exist and the corresponding method does not exist. Static classes do not exist until you reference the static class namespace.

Related Article

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.