0

What is the difference between 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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.