开发者

static variables and functions

开发者 https://www.devze.com 2023-01-25 09:52 出处:网络
What is the difference bet开发者_运维知识库ween instance and static variables/functions .Is there any performance gain using static variables.In which conditions we should use static instead of instan

What is the difference bet开发者_运维知识库ween instance and static variables/functions .Is there any performance gain using static variables.In which conditions we should use static instead of instance variables/functions. i'm not sure when I should use static variables/functions instead of instance ones


Static members belong to the class, while instance members belong to instances (objects) of that class. There will only ever be one copy of a static variable.

Methods could be made static if:

  1. They do not reference any non-static members of their class, and
  2. They are not defined to implement an interface or override a method in a superclass.

Static methods do not have the hidden this parameter, so they require less stack space. But static methods are not inherently faster.

Fields/properties should only be made static if you only want one "copy" of the field/property. If you want each object of your class to have its own copy of a field or property, it should not be static.

0

精彩评论

暂无评论...
验证码 换一张
取 消