C# - Static and Private
Discussion
Can someone tell me what the point of a method declared as both private and static within a class ?
I am doing some code refactoring and the tool wants me to declare a currently private method as static but I don't want to change it to public.
Thanks,
(I'm not making it static as there is no need for it to be static, just curious as to what logic would require a method that is both private and static..)
I am doing some code refactoring and the tool wants me to declare a currently private method as static but I don't want to change it to public.
Thanks,
(I'm not making it static as there is no need for it to be static, just curious as to what logic would require a method that is both private and static..)
Edited by him_over_there on Wednesday 16th June 10:04
Isn't this usually when there is some fixed resource at application scope?
The object provides a private static method by which it manages that resource, and then a user can create as many instances (perhaps in different thread contexts) as he/she wants. The class manages all the different instances that want access to that shared resource.
If the method was public, it would obviate the genuinely useful code in the object, and all of the users would be fighting over the single resource it represents.
I think this comes under the general description of "Marshaling, Serialisation and Synchronisation".
I'm afraid to say that I'm C++.
The object provides a private static method by which it manages that resource, and then a user can create as many instances (perhaps in different thread contexts) as he/she wants. The class manages all the different instances that want access to that shared resource.
If the method was public, it would obviate the genuinely useful code in the object, and all of the users would be fighting over the single resource it represents.
I think this comes under the general description of "Marshaling, Serialisation and Synchronisation".
I'm afraid to say that I'm C++.
Edited by dilbert on Thursday 17th June 16:57
Here's an example - Imagine you have 2 static methods A and B that do similar things. Some of the code in A and B may be the same so you might want to refactor that code into a separate method in the same class and have A and B call that method (duplication of code = bad). You should always use the most restrictive visibility applicable, so you declare a private static method.
Edited by 130R on Thursday 17th June 19:08
130R said:
Here's an example - Imagine you have 2 static methods A and B that do similar things. Some of the code in A and B may be the same so you might want to refactor that code into a separate method in the same class and have A and B call that method (duplication of code = bad). You should always use the most restrictive visibility applicable, so you declare a private static method.
It might be different for C#, but in C++, it wouldn't matter if it was static or not (except that by making it static one would have to pass a "this pointer" to it).Edited by 130R on Thursday 17th June 19:08
In C++ one of the key benefits of a static member function is that you can use it as a reference when you call a function that requires a _cdecl callback. Non-static member functions in C++ have the _thiscall calling convention and because of their decoration, the linker can (quite rightly) not manage them properly.
Typically the only time one might need that capability is when one is using a a shared system resource.
I'm pretty sure that the static keyword in C# and Java has other meanings too.
Edited by dilbert on Thursday 17th June 19:38
dilbert said:
It might be different for C#, but in C++, it wouldn't matter if it was static or not (except that by making it static one would have to pass a "this pointer" to it).
In Java and C# static methods cannot call non-static methods without an instance of the class (since a non-static method is associated with an instance). Also in Java and C# the this keyword refers to the current instance of the class, which you do not have available in a static method since a static method is not associated with an instance but the class itself. You could create a new instance in a static method or pass an instance into the method of course.130R said:
dilbert said:
It might be different for C#, but in C++, it wouldn't matter if it was static or not (except that by making it static one would have to pass a "this pointer" to it).
In Java and C# static methods cannot call non-static methods without an instance of the class (since a non-static method is associated with an instance). Also in Java and C# the this keyword refers to the current instance of the class, which you do not have available in a static method since a static method is not associated with an instance but the class itself. You could create a new instance in a static method or pass an instance into the method of course.It sounds more or less the same then.
I find that a bit weird. Whenever I've seen example Java code, the static keyword is peppered around everywhere. For me, I know the kinds of things I can do with it, and rarely find it useful. When it is useful, nothing else would do. Perhaps that's because Java and C# don't allow global functions, not associated with a class. Certainly, I'd code those as I would have in C. At least that way there's no doubt, at least not to me. At the very least you don't have to keep typing "static".
Private Static in a class - I can see a use...
You have a static class it does a bunch of stuff and has a public static method called DoYourThing(). Part of the implementation requires some functionality in another member call DoVeryDangerousThingWithParameter(string Thing)
Now you'd not want the DoVeryDange... method to just be called by anyone so you define it as Private Static. So in other words it's a way of putting the intricacies of some of your implementation in a static class and making sure it remains hidden from consumers or inheritors.
You have a static class it does a bunch of stuff and has a public static method called DoYourThing(). Part of the implementation requires some functionality in another member call DoVeryDangerousThingWithParameter(string Thing)
Now you'd not want the DoVeryDange... method to just be called by anyone so you define it as Private Static. So in other words it's a way of putting the intricacies of some of your implementation in a static class and making sure it remains hidden from consumers or inheritors.
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff