Is passing 'this' in a method call accepted practice in java

后端 未结 10 1203
无人共我
无人共我 2021-01-30 19:32

Is it good/bad/acceptable practice to pass the current object in a method call. As in:

public class Bar{
    public Bar(){}

    public void foo(Baz baz){
              


        
10条回答
  •  猫巷女王i
    2021-01-30 19:58

    There's nothing wrong with that. What is NOT a good practice is to do the same inside constructors, because you would give a reference to a not-yet-completely-initialized object.

    There is a sort of similar post here: Java leaking this in constructor where they give an explanation of why the latter is a bad practice.

提交回复
热议问题