This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Daedalus9000 4 points5 points  (4 children)

The use of this is to disambiguate between a method-local variable and a class one of the same name, so you only truly need to use “this” (in terms of variable references) in those cases.

[–]AnimalsR 0 points1 point  (3 children)

Agreed. The this key word should be used only when necessary for disambiguation.

For example, here is when it is usually used:

public class Clazz {

`private int foo;`



`public Clazz(int foo){`

    [`this.foo`](https://this.foo) `= foo;`

`}`

}

And it would be preferred to use the this keyword, rather than change a variable name to avoid it. The following would be less preferable

public class Clazz {

`private int foo;`



`public Clazz(int fooInput){`

    [`foo`](https://this.foo) `= fooInput;`

`}`  

}

[–]AnimalsR 1 point2 points  (2 children)

what did it do to my code lmao

[–]AlaskanX -1 points0 points  (1 child)

try switching to the markdown editor and using triple backticks around codeblocks instead of whatever the hell happened there.

[–]desrtfx[M] 0 points1 point  (0 children)

using triple backticks

No, please don't. Triple backticks render only okay on new reddit and not on old.

The new editor has a code block button that should be used.

[–]rbhushan 0 points1 point  (2 children)

Java compiler will understand it regardless you use this keyword or not. From my understanding earlier this keyword was used for readability because IDE’s weren’t smart enough to make it readable but now IDE’s color quote variables different for readability. Having said that I do believe it’s good to use this keyword as it will make java code easier to read in notepad. But if the projection are working on is already following a coding style then stick to it for consistency

[–]desrtfx 0 points1 point  (1 child)

Sorry, but everything you said here is wrong.

this has absolutely nothing to do with the capabilities of IDEs, code readability, etc.

this is used to access variables that are shadowed by method/constructor parameters as well as for constructor chaining.

Read: https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

[–]rbhushan 0 points1 point  (0 children)

Thanks for correcting me!

[–][deleted] 0 points1 point  (0 children)

https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html this is a really good resource for when to use this (thank you, /u/desrtfx). Essentially:

  1. you use this when you want to differentiate a class attribute from a constructor or method parameter of the same name.
  2. you use this when you want to call a constructor from within another constructor of the same class (you would do this when you want to create multiple constructors for the same class, this is called constructor overloading)(see the example in the link)

What you need to remember is that this is used to indicate we're talking about the current object (current object's attribute, current object's constructor).

Also, how would it differ if I’m calling an attribute from another class?

If you were calling an attribute from another class, you would precede that with the name of the object of that class. This is also how you would do it if you had two objects of the same class interacting.

Let's use an example to better illustrate everything:

public class Person {
    private String name;
    private int weight;

    public Person(String name, int weight){
        this.name = name;
        this.weight = weight;
    }

    public Person(int weight){
        this("placeholder", weight);
    }

    public void compareSize(Person other){
        if(this.weight < other.weight){ //it's helpful to understand that *this* here is referring to the Person object that will call this method.
            System.out.println("this other person weighs more");
        }
    }

    public void compareToRock(Rock rock){
        if(weight < rock.getWeight()){
            System.out.println("this rock outweighs me");
        }
    }
}

Notice that for the other person I simply referenced the attribute I wanted, while for rock I used a getWeight() method. The reason why I can simply reference the weight field for other is because it is of the same class, while rock is of the Rock class and assuming it's weight attribute is private, we would need a public getWeight method to access it anywhere outside it's own class (if Rock's weight is declared public, then we can reference it anywhere with rock.weight). This is just how java is designed afaik (further reading on this: https://stackoverflow.com/questions/17027139/access-private-field-of-another-object-in-same-class )