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

后端 未结 10 1330
无人共我
无人共我 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条回答
  •  青春惊慌失措
    2021-01-30 19:56

    Acceptable

    Snippet from Oracle JAVA docs:

    Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

    Using this with a Field

    The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

提交回复
热议问题