View Full Version : why private static

05-25-2007, 11:54 AM
why would you declare a class variable and method as private static?, I was looking for the implementation of singleton (http://msdn2.microsoft.com/en-us/library/ms954629.aspx) design pattern and found the use of private static member variable.

05-25-2007, 12:22 PM
Calling the method from a static reference internal to the class. It means you don't need a class instance to call the method internally. Say you've got another method you call from a static reference such as MyClass.staticMethod(); and in turn the staticMethod() calls a private method. Because the class isn't instantiated and you're calling from a static reference, then this is the only way to call a private member, as long as it's static that is.

05-26-2007, 12:15 AM
Yea, as with all private methods it would just be to help some other method.
Most if not all instance variables should be declared private or at least protected (unless you really don't care if someone changes something to god knows what). Static can provide a performance boost and is also usable from a static context. If it doesn't use an instance variable then there is no reason to make it non-static anyway.

For example if you had an Appointment class. You could have a method checkDate(Date d) and another (possibly static) suggestDate(). Both of these may need isLeapYear(int year) but you never really need to know if an appointment is in a leap year, so you wouldn't want to have myappointment.isLeapYear(1986). Adding non-sensical methods that are only there to help other methods publicly does not make sense, and there is no need for it to be non-static if you give it all the data it needs every time you call it anyway because it has nothing to do with an instance of the class.

05-31-2007, 12:50 PM
Thanks both of u, it really helps me understand why to use private static, and here I would like to share a good article (http://blogs.msdn.com/davidklinems/archive/2006/10/27/const-vs-static-readonly.aspx) on difference between const and static readonly.

05-31-2007, 02:57 PM
It is confusing, I remember being taught the concept years ago because I just didn't understand why you would need private static. I use it very occasionally, it's good if you want to modularise your code a bit more to separate certain tasks which are doing things internal to the class.

05-31-2007, 07:22 PM
Well static has its uses and private has its uses and when they overlap, private static becomes useful :)

That article seems strange to me, it is as if the author did not have the slightest idea what static or const actually do, they are by no means interchangable and represent completely different concepts.

Some of these definitions change from language to language by the way. The definitions I gave are from Java but should apply to C#. However, VB.NET is also part of the .NET Framework (to which that article refers) and static in VB.NET is completely different (Java and C#'s meaning of static is "shared" in VB.NET) It might be worth picking a language and just working out what all of the keywords you need from it are.

06-01-2007, 01:27 PM
That article seems strange to me, it is as if the author did not have the slightest idea what static or const actually do, they are by no means interchangable and represent completely different concepts.

Well, I guess thats what author was trying to explain that when used with const and then trying to change the value of constant doesn't result in expected output. As the compiler has already resolved its reference ( coz it was declared as a constant)
But when declared as static readonly the compiler did not resolve the reference and that actually solved the problem.

06-01-2007, 04:06 PM
Indeed but someone who makes that kind of mistake really shouldn't be trusted for any advice they give for programming, as they are obviously not that experienced. The freshers in my local uni wouldn't mistake static and const.

static readonly in java is pretty much this anyway:

public class Foo
private static int number = 42;

public int getNumber()
return Foo.number;
}const would be as follows (and is a lot more useful in this case)
public class Bar
public static final int number = 42;
}Note that if it is final it may as well be static in most cases as there is no need to store the same thing in each instance, just store it once for the class. (This doesn't apply to situations where final is used for inner classes, for example)

If const and static readonly were interchangable there wouldnt be keywords for both of them that can be used separately of each other, together or not at all.