Static and non-static class members

I am new to programming in general. I have a quick question - what is best for static / non-stationary variables.

I have a variable, private int x, which belongs to the class y. To access this variable, I need to access y. If x was static, I can access this variable without reference to y.

What would be the best way in a situation where several methods in class y refer to this value?

Hope this makes sense and my question is not too simple!

Thank you very much

+18
object c # oop static class
Mar 29 '12 at 11:14
source share
7 answers

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

If this variable must be the same in all instances of the class, use a static variable.

If not, use the instance variable.

In general, with public static variables, bad practice is a common global resource, and if you change it, you need to synchronize access with it. Having a global state is what you want to avoid as much as possible.

+37
Mar 29 '12 at 11:17
source share

It is best to avoid public static. In OOP, a class must hide its members. Static is actually not an instance member, but of type .

Staticity comes in handy if you use a singleton pattern. But again, they should be private and accessible through state ownership.

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

+4
Mar 29 '12 at 11:15
source share

Your choice depends on your architecture.

Static makes part of a type, others a part of an instance of that type. If you want to have some kind of common state (say) between different instances of the same type, use Static . If you want each instance to have its own value, independently of the others, use the instance fields.

In both cases, by the way, avoid expansions like public fields , but use properties.

+3
Mar 29 '12 at 11:19
source share

Well, I can’t finally say which is better, because they serve for 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 to which it belongs.

C # follows a similar principle for methods. Static methods can be accessed directly from the class, while non-static methods (or instance methods, as I 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, much less impractical (see below).

In OOP, static variables are used for values ​​that cannot be stored by an instance variable. Example: suggested that you want to keep the number of instances of the class? How can you save this in one copy?

The methods use a similar principle. They should be used for procedures for which it is not practical to do this in an instance of the class. I tend to use them for broad procedures (rather than technical terms), which means those that do not require me to instantiate an object. Example: adding two parameters. (This use may or may not be correct, but I believe it is)

However, if you want to add two properties of an object, the method cannot be static, because, as you soon realized, static methods cannot access methods or instance variables inside the class. Of course, this makes sense, because this static method would not know which instance of the class would receive them, unless that was said, since it is not part of the instance itself)

In order not to complicate the situation, I will stay here. Let me know if you understand something.

+3
Dec 14 '12 at 11:43
source share

I totally agree with Mr. Oded :

If this variable must be the same in all instances of the class, use a static variable.

If not, use the instance variable.

Yes, adding static to a class member basically means that you can access it without an instance and only outside of any instance. And yes, it will become a global resource or even a global variable if you do.

But I think there is at least one more ( heavily edited ) good moment to be a maid here ...

Using static members as global vars against OOP

This means that after setting a static member, you cannot pass it as an object. The more you use static as a global var, the more difficult it is for unit testing / mock classes

There is a Singletons solution for this. But they should never appear without warning !

On the other hand, if you are sure you really need global vars, look at the Toolbox template. This is not a very famous Singleton extension. In fact, this is so unknown, if you use Google for this, you will not find it with these keywords (toolbar template).

So, plan ahead. Read more. Learn about each option so you can decide better. Even get a book. Object-oriented programming is more about applying concepts that will help in the long run, and not just to get the job done now.

+1
Nov 15 '13 at
source share

In general, if you want to have a public variable, static or instance, you should wrap it in a property and set it like this. This is exactly the principle that you will like.

But despite some other answers, I cannot say that I am not using static. A static is not a devil that you should avoid in any case. What you need to do will decide whether you will use static or not if you keep your program clean and easy to maintain.

It is easy to say, and not in the language of elders, static means something that does not belong to any instance of this class, but affects them. An example of a static property in a class that generates instances is, for example, a factor that must be global for all instances of the class, to take part in the calculation that is performed inside the instances. In this case, and, in my opinion, it is better that this factor be declared as static, and in each instance. Especially if this factor changes the lifetime of your program in order to influence the next calculation.

0
Mar 29 '12 at 11:43
source share

You need to ask yourself: why do I need x static?

If you make x static, it means that x is part of all objects of class A, but when x is not static, it means that x is part of only one object.

In gelation, using static fields is painful for tracking errors, but in some cases it is very useful.

I suggest you familiarize yourself with using singelton http://en.wikipedia.org/wiki/Singleton

0
Mar 29 '12 at 11:43
source share



All Articles