I wonder why you use this before Android this.findViewById

Asked 1 years ago, Updated 1 years ago, 95 views

While I was reading the Android book, I had a question~

Usually, you find the ID of the view in the following way.

Button startButton = (Button) findViewById(R.id.startButton);

In the book, we put this in front of findViewById.

Is there a reason to add this? Is it a problem of readability?

Button startButton = (Button) this.findViewById(R.id.startButton);

android path

2022-09-22 12:59

1 Answers

This is used to refer to the object itself. The following code requires this.

//  Case 1
private String param;

public Construct(String param) {
  this.param = param;
}

//  //  Case 2
public Function() {
  synchronized(this) {
    ...

  }
}

This in the code you posted doesn't have any special meaning. The part where readability increases when using this is also a subjective area, so it is difficult to draw a line. I think it would be better to understand that this is the code writer's coding style in the code you asked.


2022-09-22 12:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.