Is it bad practice to make a setter return “this”?

前端 未结 27 1040
抹茶落季
抹茶落季 2020-11-27 09:39

Is it a good or bad idea to make setters in java return \"this\"?

public Employee setName(String name){
   this.name = name;
   return this;
}
27条回答
  •  感动是毒
    2020-11-27 10:09

    At least in theory, it can damage the optimization mechanisms of the JVM by setting false dependencies between calls.

    It is supposed to be syntactic sugar, but in fact can create side effects in the super-intelligent Java 43's virtual machine.

    That's why I vote no, don't use it.

提交回复
热议问题