When to use static methods

前端 未结 21 1912
旧巷少年郎
旧巷少年郎 2020-11-21 05:05

I am wondering when to use static methods? Say if I have a class with a few getters and setters, a method or two, and I want those methods only to be invokable on an instanc

相关标签:
21条回答
  • 2020-11-21 05:37

    After reading Misko's articles I believe that static methods are bad from a testing point of view. You should have factories instead(maybe using a dependency injection tool like Guice).

    how do I ensure that I only have one of something

    only have one of something The problem of “how do I ensure that I only have one of something” is nicely sidestepped. You instantiate only a single ApplicationFactory in your main, and as a result, you only instantiate a single instance of all of your singletons.

    The basic issue with static methods is they are procedural code

    The basic issue with static methods is they are procedural code. I have no idea how to unit-test procedural code. Unit-testing assumes that I can instantiate a piece of my application in isolation. During the instantiation I wire the dependencies with mocks/friendlies which replace the real dependencies. With procedural programing there is nothing to "wire" since there are no objects, the code and data are separate.

    0 讨论(0)
  • 2020-11-21 05:39

    Define static methods in the following scenarios only:

    1. If you are writing utility classes and they are not supposed to be changed.
    2. If the method is not using any instance variable.
    3. If any operation is not dependent on instance creation.
    4. If there is some code that can easily be shared by all the instance methods, extract that code into a static method.
    5. If you are sure that the definition of the method will never be changed or overridden. As static methods can not be overridden.
    0 讨论(0)
  • 2020-11-21 05:39

    Static methods should be called on the Class, Instance methods should be called on the Instances of the Class. But what does that mean in reality? Here is a useful example:

    A car class might have an instance method called Accelerate(). You can only Accelerate a car, if the car actually exists (has been constructed) and therefore this would be an instance method.

    A car class might also have a count method called GetCarCount(). This would return the total number of cars created (or constructed). If no cars have been constructed, this method would return 0, but it should still be able to be called, and therefore it would have to be a static method.

    0 讨论(0)
  • 2020-11-21 05:40

    No, static methods aren't associated with an instance; they belong to the class. Static methods are your second example; instance methods are the first.

    0 讨论(0)
  • 2020-11-21 05:41

    A static method is one type of method which doesn't need any object to be initialized for it to be called. Have you noticed static is used in the main function in Java? Program execution begins from there without an object being created.

    Consider the following example:

     class Languages 
     {
         public static void main(String[] args) 
         {
             display();
         }
    
         static void display() 
         {
             System.out.println("Java is my favorite programming language.");
         }
      }
    
    0 讨论(0)
  • 2020-11-21 05:41

    I am wondering when to use static methods?

    1. A common use for static methods is to access static fields.
    2. But you can have static methods, without referencing static variables. Helper methods without referring static variable can be found in some java classes like java.lang.Math

      public static int min(int a, int b) {
          return (a <= b) ? a : b;
      }
      
    3. The other use case, I can think of these methods combined with synchronized method is implementation of class level locking in multi threaded environment.

    Say if I have a class with a few getters and setters, a method or two, and I want those methods only to be invokable on an instance object of the class. Does this mean I should use a static method?

    If you need to access method on an instance object of the class, your method should should be non static.

    Oracle documentation page provides more details.

    Not all combinations of instance and class variables and methods are allowed:

    1. Instance methods can access instance variables and instance methods directly.
    2. Instance methods can access class variables and class methods directly.
    3. Class methods can access class variables and class methods directly.
    4. Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
    0 讨论(0)
提交回复
热议问题