Nes370
Nes370

Reputation: 23

Is the use of the "this" keyword superfluous? Java

I've been doing some research here and there on the internet, and I think I almost understand how the this keyword operates. My question is: When is this a necessary function?

I know that it is useful for distinguishing between an Object's private instance variable and another variable that it's being set to if they have the exact same name. Example:

public class SomeObject {
    private int someNum;
    public SomeObject() {}
    public setSomeNum(int someNum) {
        this.someNum = someNum;
    }
}

I also learned from the second answer @using-the-keyword-this-in-java that it is used for accessing a "nested" class. Although I have yet to try this out, it appears to be a pretty cool idea.

How this question came to be about: I am writing a class that defines game characters from a bunch of attributes:

public class Charact {
    private String name;
    // A ton of other private variables that represent character stats.
    public Charact() {} // This one is for the main character.
    public Charact(String name, /*All of the other variable parameters*/) { // This one is used for making NPCs.
        this.name = name;
        // Setting of all of the other variables.
    }    
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    // All of the other getter and setter methods.
}

So here's a few questions about my code (that are tangential to my initial question):

Is calling the setter methods with a this, like in this.setVariable(variable);, superfluous because it would do the same thing as setVariable(variable);?

I specifically declared all of my private instance variables with an uppercase letter, allowing me to avoid the use of this in my setters. Is it alright for me to do so (am I breaking any normal naming conventions?) or should I have ought to use lowercase variable names and thises in the setters?

Does using this somehow help identify which instance of a class is being referred to, or is it already implicit to the computer, meaning that this is not really needed?

Any answers that can offer some depth of elaboration would be very helpful, as I've only started making non-static classes this last week. Also, this is my first question, so I am sorry if it doesn't really seem to be asked in the right formatting.

Upvotes: 2

Views: 760

Answers (3)

CharlieS
CharlieS

Reputation: 1452

calling the setter methods with a this, like in this.setVariable(variable) is usually superfluous, but good practise.

Declaring any variables with an uppercase letter is confusing for all other java programmers who might have to maintain your code. We adopt conventions for good reasons. It will confuse you too eventually, when all your variables look like classes, and you can't tell your static calls from instantiated.

this is implicitly this object.

Edit: above sentence changed from this class

Upvotes: 1

Radiodef
Radiodef

Reputation: 37845

I think you already have a grasp on what this is for. It is available when you need to be explicit about what you are referring to.

Here is a novel example. (Don't actually write a class like this. It just serves to show usage of this.)

class Outer {
    int value; /* #1 */

    class Inner {
        int value; /* #2 */

        void setInnerValue(int value /* #3 */) {
            //    #2      #3
            //   vvvvv   vvvvv
            this.value = value;
        }

        void setOuterValue(int value /* #4 */) {
            //          #1      #4
            //         vvvvv   vvvvv
            Outer.this.value = value;
        }
    }
}

If there is no need to qualify what you are referencing, then yes it is superfluous.

this is also used to invoke constructors in a chain.

class Example {
    Example() { this(1); }
    Example(int i) { }
}

am I breaking any normal naming conventions?

Yes, you are. Identifiers in Java start with a lowercase letter by convention (unless they are static final constants which are generally all uppercase). You should follow the convention.

Upvotes: 4

Gilian Joosen
Gilian Joosen

Reputation: 486

It's handy to use this, because later you will have things like super. and if you don't use super there and don't use this you will end up with messy code.

Upvotes: 0

Related Questions