0

Consider this:

public class Test {

    public static int numberOfInstances = 0;
    public int myInstanceID;
    public String myInstanceName;

The static variable doesn't need to be called within an instance, it's available everywhere like this:

Test.numberOfInstances

When creating an instance, I only do this into my constructor:

public Test(int id, String name) {
    myInstanceID = id;
    myInstanceName = name;
    numberOfInstances += 1;
}

I've recently discovered the this keyword and have noted some of its uses:

public Test() {
    this(numberOfInstances + 1, "newInstance");
    numberOfInstances += 1;
}

From what I've noticed, the this keyword allows you to call another one of the class' constructors. It also allows you to do this:

public Test(int x, int y) {
    this.x = x;
    this.y = y;
}

With java, I highly disagree with this style; same variable names, and I don't see the point of using this, especially after looking at the docs example. I look at this:

public Test(int a, int b) {
    x = a;
    y = b;

However, the use of the this keyword isn't necessary; In my code, I have a variables in my class (e.g. xCoordinate) where I don't use the this keyword (it's not static).

What I've been struggling to understand is what the difference is between non-static variables and this variables. Is there a difference? In one of my classes (the Paddle for Pong), I have this:

public class Pong {
    public int xCoordinate;
    public int yCoordinate;

and so on... I never use the this keyword anywhere, and the data is stored within it's own instance.

Bottom line, my question is what is the difference between non-static variables and this.variables. Is it a standard coding practice? Why would I ever you the this keyword on non-static variables?

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • possible duplicate of [java "this" keyword proper use](http://stackoverflow.com/questions/28805295/java-this-keyword-proper-use) – dsolimano Apr 16 '15 at 15:31
  • 1
    "non-static class variable" is awkward, the usual terminology is "instance variable". – Nathan Hughes Apr 16 '15 at 15:37
  • Sorry, coming from Python and being relatively new to Java, it's quite confusing for me... I've tried though. If you want, you can edit it to "correct" terms. And I don't think the question's a duplicate; I'm not looking for uses, but rather what the differences in syntax and code styles are. In Python, not everything is in a class, and we use the word `self` to basically distinguish unique variables to instances. – Zizouz212 Apr 16 '15 at 15:40

5 Answers5

1

If, as is typical, the parameter variable names of a constructor (say x) are the same as fields of the class, then the field names are shadowed by the parameters passed.

this is used in this case to disambiguate: this.x denotes the field x. It makes perfect sense. this means "reference to the current instance".

So, statements like this.x = x; are quite common.

If you still continue to dislike the Java style, and you adopt m_x-style notation for class fields, then you can write m_x = x; in your constructor. As you rightly point out, this is then not required.

this is also used as the notation for delegating constructors, as you point out.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

The "this" keyword allows you to difference between method and instance variables:

public class Point {
    private int x;
    private int y;  

    public void add(int x, int y) {
        this.x += x;
        this.y += y;
    }
}
FuRioN
  • 623
  • 5
  • 12
1

I think you may have almost answered your own question. You provided the function

public Test(int x, int y) {
    this.x = x;
    this.y = y;
}

However, what do you think would happen if you wrote it this way instead?

public Test(int x, int y) {
    x = x;
    y = y;
}

Noticed that I removed the this in the second function. Therefore, x and y would just be referring to the local x and y variables. this allows you to specify that you actually want to use the non-static class variables x and y.

Justin L
  • 375
  • 2
  • 10
  • 1
    Thanks to everyone for the help! All of your answers were extremely helpful to me! For each one that has helped me out, I've given them an up vote. Thank you so much guys! – Zizouz212 May 16 '15 at 01:35
1

There is no this variables. It's just used to tell the compiler that the variable you want to change is the declared field and not the local variable, in case they have the same name.

For the constructor part, this is just a shortcut for classes which have multiple constructors. You can write the code once and just call that from the alternative constructors.

There is also a similiarly used keyword super, which allows you to call methods and constructors of the superclass:

public SomeClass(int x) {
    super(x);
    super.someMethod(); // even if we would have overridden someMethod(),
                        // this will call the one from the superclass
}
Bubletan
  • 3,833
  • 6
  • 25
  • 33
0

Here's one instance where you would need the 'this' keyword:

public class Pong {
    public int xCoordinate;
    public int yCoordinate;

    public Pong (int xCoordinate, int yCoordinate) {
        this.xCoordinate = xCoordinate;
        this.yCoordinate = yCoordinate;
    }
}
Dave
  • 363
  • 2
  • 9