Some misunderstandings about static methods and instance methods.

Source: Internet
Author: User

Xianfo
[Transferred from ivony's blog]

Some misunderstandings about static methods and instance methods.

I. Static methods are resident in memory, and the instance method is not. Therefore, static methods are highly efficient but occupy 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.


2. The static method allocates memory on the stack, and the instance method is on the stack.

In fact, all methods cannot allocate memory on the heap or stack. Code Is loaded to a special code memory area, which cannot be written.


3. instance methods can be called only after an instance is created. Static methods are not needed and simple.
In fact, if a method has nothing to do with its instance type, it should be static, and no one will write it as an instance method. Therefore, all instance methods are related to instances. Since they are related to instances, creating an instance is an inevitable step. In practice, you can write all the instance methods as static ones, and pass the instance as a parameter.
Some methods seem unrelated to the instance, such as icomparer. compare method, but in fact every class that implements this interface will only be responsible for comparing its own type instances. This is C #1. the X specification does not have any historical issues caused by generics.
Most static methods are related to the instance of the class, such as various parse methods. The reason why the static method is made is that it does not have an instance as a parameter. Most of the rest are 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.