Accessor Method Performance and Optimization

前端 未结 6 2413
有刺的猬
有刺的猬 2021-02-15 12:11

Often, I come across code where the Getter method is repeatedly used/abused to get some value or pass it as a method parameter, for ex:

public c         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-15 12:58

    If your reasoning for doing something in Java is because "it might perform better", you're doing it wrong. The JIT compiler will optimize the trivial case public String getName(){return name;} away. Instead, you should be making decisions based on, "is this the right OOP way to do it"

    From an OOP standpoint, only use the getter. Then, a subclass can override the method as needed to do other fancy things (like ignore the name "Einstein").

提交回复
热议问题