When should I write Static Methods?

后端 未结 8 720
梦如初夏
梦如初夏 2021-01-04 02:31

So I understand what a static method or field is, I am just wondering when to use them. That is, when writing code what design lends itself to using static methods and field

相关标签:
8条回答
  • 2021-01-04 02:53

    You should use static methods whenever you have a function that does not depend on a particular object of that class.

    There is no harm in adding the static keyword: it will not break any of the code that referred to it. So for example, the following code is valid whether or not you have the 'static' keyword:

    class Foo
    {
        public Foo(){}
        public static void bar(){}  // valid with or without 'static'
        public void nonStatic(){ bar(); }
    }
    
    ...
    Foo a = new Foo();
    a.bar();
    

    So you should add 'static' to whatever methods you can.

    0 讨论(0)
  • 2021-01-04 02:54

    Use a static method when the method does not belong to a specific object.

    For example, if you look at the Math class in .NET framework, you will see that all methods are static. Why? Because there is no reason to must create an object to use the methods. Why would you want to create an object of the Math class, when all you want is the absolute value of something? No, there is no reason to do this, and therefore, the method is static.

    So when you design a class, ask yourself:

    Does this method belong to an object, or the class itself?

    A method belongs to an object, if it modifies the state of the object. If the method does not modify a specific object, it can most likely be static.

    Another example, suppose that you want to know how many objects of a class that is created (don't ask me why...). For this task, you could create a static method GetNumberOfObjects() (and you obviously need a static field, and some code in the constructor too). Why would i have it static, you might ask. Well, answer the above question, and you will see. The method does not belong to any specific object. Additionally, it does not modify any object.

    I hope this makes sense.

    0 讨论(0)
  • 2021-01-04 02:56

    Static methods are usually useful for operations that don't require any data from an instance of the class (from this) and can perform their intended purpose solely using their arguments.
    A simple example of this would be a method Point::distance(Point a, Point b); that calculates the distance between two points and don't require an instance.

    Static fields are useful among others for constants that don't change all that often and are used by all the instances of a class.

    0 讨论(0)
  • 2021-01-04 03:05

    I would say use static methods whenever you have functions which are independent of the state of the instance, ie. doesn't depend on any instance fields.

    The less non-local state that a method depends on, the easier it is to understand, so static is a helpful signal to the reader of the code.

    0 讨论(0)
  • 2021-01-04 03:06

    I keep it clear by remembering that instance methods work on/inside individual objects while static methods do something for the Class.

    In the case of LoadFromFile(), you want a static method because you want a null reference if the load fails - the instance doesn't exist yet. If you implemented it as a constructor, you'd have to throw an Exception on failure.

    Other good uses for statics: Compare(obj a, obj b), Delete(obj a) for data objects (an object can't delete itself since its reference is still around), or static Classes for procedural code that honestly can't be modeled in an object.

    0 讨论(0)
  • 2021-01-04 03:07

    You may use static methods when the client of the class do not have an instance of the class to work with. For instance the Singleton design pattern is used to ensure that only one instance of a class exist in the system. It requires that the constructors of the Singleton be private so that no instances can be created by the client.

    So if you cannot create an instance how do you access the instance methods of the class? By calling a static method that returns the Singleton instance of the class.

    This is of course just one scenario but there are many others.

    0 讨论(0)
提交回复
热议问题