Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static vs non-static class members

I'm new to c sharp and programming generally. I have a quick question - what is best practise with regards to static/non static variables.

I have a variable,private int x, which belongs to class y. To access this variable, i need to reference y. If x was static however, i can access this variable with no references to y.

Which is the best way to go, in a situation whereby several methods within the class y will be referencing this value ?

Hope this makes sense, and my question isn't too basic !

Many thanks

like image 679
Sherlock Avatar asked Mar 29 '12 11:03

Sherlock


People also ask

What is the difference between static and non static members?

static members are accessed by their class name which encapsulates them, but non-static members are accessed by object reference. static members can't use non-static methods without instantiating an object, but non-static members can use static members directly.

What is non static member?

A non-static member function is a function that is declared in a member specification of a class without a static or friend specifier. ( see static member functions and friend declaration for the effect of those keywords)


4 Answers

You need to think about static variables as belonging to the class, not to instances of the class.

If, in all instances of the class this variable should be identical, use a static variable.

If not, use an instance variable.

In general having public static variables is bad practice - it is a shared global resource and if you change it you need to synchronize access to it. Having global state is something you want to avoid as much as possible.

like image 115
Oded Avatar answered Oct 01 '22 19:10

Oded


Best practice is to avoid public static. In OOP, class is meant to hide its members. Static is actually not a member of the instance but of the type.

Static comes handy if you are implementing singleton pattern. But then again they need to be made private and accessible through a public property.

You need to read Static Classes and Static Class Members (C# Programming Guide).

like image 20
Aliostad Avatar answered Oct 01 '22 19:10

Aliostad


Well I can't conclusively say that one is better, because they serve different purposes.

Are you familiar with OOP? In OOP, static objects or members of a class that can be accessed directly from the class, while non-static members can only be accessed from the instance it belongs to.

C# follows a similar principle for the methods. The static methods can by accessed directly from the class, while non-static methods (or instance methods as I like to call them) have to be accessed from an instance. That is why instatiating needs to be done for instance methods, while for static methods it's just not needed, and furthermore impractical (see below).

In OOP, static variables are used for values which cannot be stored by an instance variable. Example: supposed you wanted to keep a count of how many instances of a class exists? How would you store that in a single instance?

The methods use a similar principle. They should be used for procedures for which it is impractical to do within an instance of a class. I tend to use them for broad procedures (not a technical term), meaning those that do not require me to instantiate an object. Example, adding two parameters. (This usage may or may not be correct, but I believe it is)

However, if you wanted to add two properties of an object, the method cannot be static, because as you would soon realize, static methods cannot access instance methods or variables within a class. Of course that makes sense because that static method would not know which instance of the class the get these from unless it were told, since it is not part of an instance itself)

For the sake of no further complicating things, I'll stop here. Let me know if you misunderstood anything.

like image 4
Arash Afshinfar Avatar answered Oct 01 '22 21:10

Arash Afshinfar


Your choice depends on your architecture.

Static makes part of a Type, others make part of an instance of that type. If you want have some shared state (say) between different instances of the same type, use static. If you want that every instance have it's own value, independent from others, use instance fields.

In both cases, by the way, avoid to expose like a public fields, but use properties.

like image 3
Tigran Avatar answered Oct 01 '22 19:10

Tigran