Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    why private static

    why would you declare a class variable and method as private static?, I was looking for the implementation of singleton design pattern and found the use of private static member variable.

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    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.

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks both of u, it really helps me understand why to use private static, and here I would like to share a good article on difference between const and static readonly.

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    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.

  • #7
    New Coder
    Join Date
    Dec 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ghell View Post
    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.

  • #8
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    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:

    Code:
    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)
    Code:
    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.
    Last edited by ghell; 06-01-2007 at 03:11 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •