When to use static methods

前端 未结 21 1907
旧巷少年郎
旧巷少年郎 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:17

    A static method has two main purposes:

    1. For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.
    2. For the state that is shared by all instances of the class, like a counter. All instance must share the same state. Methods that merely use that state should be static as well.
    0 讨论(0)
  • 2020-11-21 05:22

    Use a static method when you want to be able to access the method without an instance of the class.

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

    Whenever you do not want to create an object to call a method in your code just declare that method as static. Since the static method does not need an instance to be called with but the catch here is not all static methods are called by JVM automatically. This privilege is enjoyed only by the main() "public static void main[String... args]" method in java because at Runtime this is the method Signature public "static" void main[] sought by JVM as an entry point to start execution of the code.

    Example:

    public class Demo
    {
       public static void main(String... args) 
       {
          Demo d = new Demo();
    
          System.out.println("This static method is executed by JVM");
    
         //Now to call the static method Displ() you can use the below methods:
               Displ(); //By method name itself    
          Demo.Displ(); //By using class name//Recommended
             d.Displ(); //By using instance //Not recommended
       }
    
       public static void Displ()
       {
          System.out.println("This static method needs to be called explicitly");
       }
    } 
    

    Output:- This static method is executed by JVM This static method needs to be called explicitly This static method needs to be called explicitly This static method needs to be called explicitly

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

    Actually, we use static properties and methods in a class, when we want to use some part of our program should exists there until our program is running. And we know that, to manipulate static properties, we need static methods as they are not a part of instance variable. And without static methods, to manipulate static properties is time consuming.

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

    If you apply static keyword with any method, it is known as static method.

    1. A static method belongs to the class rather than object of a class.
    2. A static method invoked without the need for creating an instance of a class.
    3. static method can access static data member and can change the value of it.
    4. A static method can be accessed just using the name of a class dot static name . . . example : Student9.change();
    5. If you want to use non-static fields of a class, you must use a non-static method.

    //Program of changing the common property of all objects(static field).

    class Student9{  
     int rollno;  
     String name;  
     static String college = "ITS";  
    
     static void change(){  
     college = "BBDIT";  
     }  
    
     Student9(int r, String n){  
     rollno = r;  
     name = n;  
     }  
    
     void display (){System.out.println(rollno+" "+name+" "+college);}  
    
    public static void main(String args[]){  
    Student9.change();  
    
    Student9 s1 = new Student9 (111,"Indian");  
    Student9 s2 = new Student9 (222,"American");  
    Student9 s3 = new Student9 (333,"China");  
    
    s1.display();  
    s2.display();  
    s3.display();  
    }  }
    

    O/P: 111 Indian BBDIT 222 American BBDIT 333 China BBDIT

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

    One rule-of-thumb: ask yourself "Does it make sense to call this method, even if no object has been constructed yet?" If so, it should definitely be static.

    So in a class Car you might have a method:

    double convertMpgToKpl(double mpg)
    

    ...which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But this method (which sets the efficiency of one particular Car):

    void setMileage(double mpg)
    

    ...can't be static since it's inconceivable to call the method before any Car has been constructed.

    (By the way, the converse isn't always true: you might sometimes have a method which involves two Car objects, and still want it to be static. E.g.:

    Car theMoreEfficientOf(Car c1, Car c2)
    

    Although this could be converted to a non-static version, some would argue that since there isn't a "privileged" choice of which Car is more important, you shouldn't force a caller to choose one Car as the object you'll invoke the method on. This situation accounts for a fairly small fraction of all static methods, though.

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