-3

According to this highly voted answer on SO Python made it mandatory to declare self in instance methods as the language designer wanted to prevent the deduction of instantiating object like done in Java through this.

What is meant by "this can be deduced in Java?"

Community
  • 1
  • 1
Inquisitive
  • 7,476
  • 14
  • 50
  • 61

2 Answers2

3

If you reference an identifier without a qualifier in Java (eg, something), the compiler will check whether there is a local variable with that name, and, if not, will deduce that it must be a field.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

I believe the original post meant that in some instances Java requires 'this' to be used to make it clear what variable you are referring to. For instance:

public class A {

    private String b;
    private String c;

    public void setB(String b) {
        this.b = b; // <-- this removes confusion for Java
    }

    public void setC(String _c) {
        c = _c; // <-- this is not needed here
    }

}
Daedalus
  • 1,667
  • 10
  • 12