Is it wrong to use this function in Java?

I'm just curious to use this because the Android code sample and documentation don't use the this (I like to believe that Android engineers are usually smart, so mine using them as a basis). I'm just wondering why development engineers don't use this lot.

+4
source share
6 answers

This is mainly a stylistic difference. I still do not see arguments in favor of why this is good or bad. Personally, I like to use it, and there are some examples when you need to use it.

For example, if you have an Anonymous Inner class that wants to call the method of the parent class, you need this . Example:

 class MyClass{ protected void doSomething(){ } OnClickListener n = new OnClickListener(){ public void onClick(View v){ MyClass.this.doSomething(); } }; } 

Another reason for using this is an argument to a method that masks a member variable. In this case, you can use this to distinguish between the two, although I would recommend that you rename the argument to something that is not masked.

 class MyClass{ protected int val; public void setVal(int val){ this.val = val; // set the member variable equal to the method argument } } 

Another reason for using this is to pass a pointer to the current instance of the class from that instance. An example of this is the creation of a new Intent

 Intent i = new Intent(this, NewActivity.class); 

There are probably more places where you will need to use this . These are the first few that come to mind.

+7
source

It doesn't matter, but if you use 'this' in your code, be sure to use it everywhere. It makes your code clear.

+1
source

Why do you think this is so? For instance. The SDK coding style guide does not mention this, and I have seen that it is used in different places. However, following the directions for variable names, this is often not necessary and may be omitted, which often happens.

Referring to an instance using this keyword is useful for clarity, but since the document mentions that there is no really coercive style, and following the naming rules is not necessary.

eg. a valid setter after naming with and without it can be

  private int mCounter; public void setCounter(int counter) { mCounter = counter } private int increment; public void setIncrement(int increment) { this.increment = increment } 

Please note that in the first example you could, but should not use this. In the second, you need the first and probably closer to the style guide.

+1
source

This is a more personal taste than bad practice.

0
source

No, no, there are links to 'this' in the Android code example, but there is no reason to think of it as bad practice. In most cases, this is unnecessary, and perhaps omitted for brevity.

For example, a call to A () from a specific class can be called as this.methodA (). This is not necessary in this case, as it is implied when making a call. It is commonly used in setters, where the argument name matches the variable name that you set.

 public void setName(String name) { this.name = name; } 
0
source

Using this is more of a safety measure:

 class MyClass{ void doSomething() { } OnClickListener n = new OnClickListener() { void doSomething() { } public void onClick(View v){ MyClass.this.doSomething(); // MyClass#doSomething() this.doSomething(); // OnClickListener#doSomething() doSomething(); // OnClickListener#doSomething() if present, // MyClass#doSomething() otherwise - danger! } }; } 

Since there are cases where you cannot control the presence of a member or method (for example, when OnClickListener is a third-party class), in order to get predictable behavior, you should always qualify such a link. If variable behavior is not exactly what you want.

0
source

All Articles