Static Method and instance method contact and difference, static instance contact difference

Source: Internet
Author: User

Static Method and instance method contact and difference, static instance contact difference

1. Call Method

The static method depends on the class and is called through the class. static method. The instance method depends on the class object. After the object needs to be created, the object. instance method is used.

2. Usage

Static variables cannot be defined inside the instance method, and compilation errors may occur. Static methods can be called directly in the instance method; instance variables can be defined and used inside the static method, the static method cannot directly call the instance method (because the class has not been instantiated when the static method is loaded, the instance method depends on the class Object)

3. Memory Allocation for static and instance methods

It is generally considered that the static method is resident memory, and the instance method allocates memory only when used. But in fact, they are all in the memory when the class is loaded. When creating an instance object, the static method and non-static method have different attribute values for each object (all fields of each instance object have a copy in the memory, used to differentiate the fields of objects to be operated.) When a new instance is added, the instance attribute is placed in GC Heap, and the new object is placed on the stack. The stack pointer points to the copied memory address; the static fields in the static method only have one copy. The static and non-static method code is the same. Therefore, only one copy of code is required, occupying one portion of memory space. In terms of calling speed, the instance method needs to be instantiated and allocated memory, but the static method is not needed, so the static method will be faster, but it is negligible.

4. Identify the causes of static methods and instance methods

In the early stage of structured programming, almost all methods were static methods. The concept of introducing instantiation methods was that after the emergence of object-oriented concepts, distinguishing static methods and instance methods could not be simply understood in terms of performance. The instantiation method introduced by masters of object-oriented languages such as c ++, c #, and java is not designed to solve performance and memory problems, but to make development more modeled and object-oriented. In this case, the distinction between static methods and instantiation methods is to solve the pattern problem.

5. Applicable scenarios

Static Method: A method has nothing to do with class objects, such as tool classes.
Example Method: A method depends on the class object. In multi-threaded scenarios, if the static method contains static variables, changing static variables may cause thread security problems.

6. Static Method and Singleton Mode

If a method has nothing to do with the instance of the object, the static method is used. Otherwise, the instance method is used. However, if the method is related to the object instance and you want to maintain an instance, you need to use the singleton mode. For example, when the system is running, some configurations and attributes need to be loaded, which are public and must exist throughout the lifecycle, however, these configurations and attributes are obtained through the object-oriented encoding method. Although static methods can also be used, the best method is the singleton mode.

Objects generated in the static method are released after the static method is executed. When the static method in the execution class is executed, the class of the static method is not instantiated. If singleton is used, the unique instance generated will remain in the memory and will not be purged by GC (because static attribute variables will not be purged by GC)

 

Reference: http://blog.csdn.net/sx5273/article/details/54342201

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.