The difference between static and non-stationary elements?

Possible duplicate:
What is a static method in C #?

It was difficult for me to understand the actual concept of static and non-static (instances) of members, after studying many forums, I decided to ask my question here:

What is the difference between static and non-stationary members?

+8
source share
2 answers

static methods can be accessed directly from the class, while non-static methods (or instance methods, as I like to call them) must be accessible from the instance. This is why instatiating needs to be done, for example, with methods, while for static methods this is simply not necessary.

In OOP, static variables are used for values ​​that cannot be stored by an instance variable. static methods cannot access instance methods or variables inside a class. Of course, this makes sense, because this static method would not know which instance of the class we are trying to pass.

eg. It was assumed that you would like to count the number of instances of the class? How can you save this in one copy?

References:

+10
source
  • static members are one for each class, but non-static members are one instance.

  • static members access their class name, which encapsulates them, but non-stationary elements are referenced by an object reference.

  • static members cannot use non-static methods without instantiating an object, but non-static members can use static elements directly.

  • static constructor used to initialize static fields, but non-static fields use a standard instance instance.

  • See here for performance related issues.

+6
source

All Articles