• Post Reply Bookmark Topic Watch Topic
  • New Topic

keyword this is not needed  RSS feed

 
paul a carron
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have this piece of code in my Java program:



I also have a code check xml which checks the format of my Java code. The xml contains the following check which prevents me fromm using the this keyword:


Is there any way I can edit my program to work without using this?
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
way I can edit my program to work without using this?

Change  the variable names so that they have different names.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the usual convention is that what you wrote originally is a good form for a constructor or setXX method, the XML check is incorrect and (I think) ought to be changed.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Since the usual convention is that what you wrote originally is a good form for a constructor or setXX method, the XML check is incorrect and (I think) ought to be changed.

I agree with this. you should change your configuration so that that warning no longer appears. It is bad advice.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the general case for referencing class fields?  Should this be warned against here?



Note: by field I do NOT mean a static variable.  I mean a field that belongs to an instance of the class.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any risk of confusion if you omit this? If so, then you shou‍ld use this. Otherwise I am neutral about it. I think this.someMethod(...); may sometimes be easier to understand than simply someMethod(...);
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:What about the general case for referencing class fields?

In your given code snippet there are no class fields. There is a field with package private access modifier. You might wanted to write "static String aVar ...".
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I did not mean a static variable.  I would have coded static if I wanted that.  I meant to code a field that was part of an instance of the class.
From the tutorial:

Variables

As you learned in the previous lesson, an object stores its state in fields.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also have a code check xml which checks the format of my Java code. The xml contains the following check which prevents me fromm using the this keyword: 

Am I right that you cannot change this XML file? 
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am one of those people that use this. all the time when referencing instance variables.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!