Using 'this' Identifier In Java

N4g4rok

Senior member
Sep 21, 2011
285
0
0
I understand what 'this' is meant to accomplish, but I'm sort of confused as far as the conventions for using it. normally, if I'm referring to anything in a class, i precede it with 'this'. for example:

Code:
public class linkedList {
    
    private class listNode
    {        
        private Object item;
        private listNode next;
        private int size;
        
        listNode()
        {
            this.item = null;
            this.next = null;
            this.size = 0;
        }
        listNode(Object _item, listNode _next)
        {
            this.item = _item;
            this.next = _next;
            size = 0;
        }
   }
to summarize my question, what is the major difference in 'this.size' or just using 'size' to refer to that variable?
 

smakme7757

Golden Member
Nov 20, 2010
1,487
1
81
you can use "this" to specify that the varialbe is already declared in the current class if you are also using a New variable With the same name

For example:

Code:
class somclass{
String text = "";
 
public somefunction(String text){
this.text = text;
 
/*this.text refers to the text variable outside the function, while text refers to the text parameter within the function*/
}
You Write "this.text" to tell the program to put the input (which is also called text in the function somefunction) into the class variable text.

Just like you have done in your program.
 
Last edited:

BrightCandle

Diamond Member
Mar 15, 2007
4,762
0
76
No difference whatsoever. The point of the this keyword is more for disambiguation of the scope of a variable (whether it belongs to the object or the method/constructor) and for return the object itself outside of the class such as in a return statement.

Typically this is used for an attribute in the constructor and any set methods so that the name can be the same. Its better to do this than use _name as the underscore is unnecessary and the automated tools will produce it.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
The only time I use 'this' is in a constructor.

Code:
public class Example {
    private final int value;

    public Example(final int value) {
        this.value = value;
    }
}
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I know people on the .net side who put 'this.' in front of every class member reference. It bugs me :). It's rarely needed, other than for ctor calls as Leros pointed out, or for disambiguation in certain circumstances.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
I know people on the .net side who put 'this.' in front of every class member reference. It bugs me :). It's rarely needed, other than for ctor calls as Leros pointed out, or for disambiguation in certain circumstances.

I would argue that you shouldn't be naming variables such that disambiguation is required. It makes sense in a constructor since you have a one to one mapping of the parameters to class fields.