Static and non-static methods

Source: Internet
Author: User

Learn the basic knowledge step by step:

Intuitively: static modifiers are available for static methods, but not for non-static methods.

 

I. Differences between static and non-static members

  1. Static members belong to the class, rather than static members belong to the class instance.
  2. Non-static members: each time a class instance is created, a storage space is opened for non-static members in the memory.
  3. Static members: no matter how many class instances are created, the static members of the class only occupy the same region in the memory.

Ii. Differences between static and non-static methods

  1. Static Methods belong to the class, rather than static methods belong to the class instance.
  2. A static method can only be a static member of the primary class, rather than a static method can be any member of the primary class.
  3. Static methods can be used before instantiation. The non-static variables in the class must be instantiated before memory can be allocated.
    C # The memory address used by non-static variables cannot be determined during static method calls. So it cannot be used. The static variable address is fixed for the class, so it can be used.
  4. Static methods are more efficient than instantiation. The disadvantage of static methods is that they cannot be destroyed automatically, while instantiated methods can be destroyed.
  5. The static method and static variable always use the same memory after being created, and multiple memories will be created using the instance method.
  6. The tatic method is a member method in the class and belongs to the entire class, that is, it can be called directly without creating any object!
    Only static variables and other static methods can appear in static! In addition, the keyword such as this... cannot be used in the static method because it belongs to the entire class!
  7. A static method is a special member method. It does not belong to a specific instance of the class, but to the class itself. Therefore, you do not need to create a class instance for the static method, but use the class name. Static Method format.

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.