2

I (think) static variables are used when you want some attribute of a class to be shared among all of its objects.

class Person{

    private String name;
    private static int qtdone = 0;
    private static int qtdtwo = 0;

    //constructor

    public Person(){
        this.name = "Generic";
        this.qtdone ++;
        qtdtwo++;
    }

    public void print_qtd(){
        System.out.println("this.qtdone is: " + this.qtdone);
        System.out.println("qtdtwo is: " + this.qtdtwo);

    }
}

public class Main {
    public static void main(String [] args) {
        Person one = new Person();
        Person two = new Person();

        one.print_qtd();
    }
}

returns

this.qtdone is: 2
qtdtwo is: 2

Which is what I expected, since qtdone and qtdtwo are modified by both "Person one" and "Person two"

What i'm not sure is the difference between this.qtdone and qtdtwo. They ended up doing the same, but I would like to confirm if they are the same or are, in fact, doing similar (but distinct) things.

abcson
  • 157
  • 9
  • 8
    no difference, `this` is unnecessary and misleading here. – Nathan Hughes Feb 28 '19 at 13:50
  • 1
    There is no difference whatsoever, but you're supposed to use `qtdtwo` instead of `this.qtdtwo` for clarity – Ayrton Feb 28 '19 at 13:50
  • They are doing the same thing, the `this` keyword is important for non-static variables and nested classes. – devgianlu Feb 28 '19 at 13:50
  • Thank you! Please, someone post the comment as an answer so i can close the topic – abcson Feb 28 '19 at 13:51
  • 2
    for when you *should* use `this` see here: https://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class – Nathan Hughes Feb 28 '19 at 13:52
  • `this` should be used in cases eg. when you have the same class variable name as method parameter, imagine then body of method, if you will assign value, it will be looking like `a=a` (then noone will know, if first 'a' is from class or method parameter), so you will use `this.a=a` – xxxvodnikxxx Feb 28 '19 at 13:56
  • @xxxvodnikxxx never use 'this.' for a static variable, not even in the use case you describe. in that case just rename the local variable (or call it through ThisClass.variable). Using this.staticVariable will make it look to people reading/maintaining your code that they are looking at an instance variable. Happy debugging :) – Stultuske Feb 28 '19 at 13:58
  • @Stultuske in ANY IDE if you will generate getters and setters, its having `this` in body :), yes, as you mentioned, never use it in case of static variables, but for getters and setters yes, basically its only case, where it is fine to have, ah plus constructors – xxxvodnikxxx Feb 28 '19 at 14:04
  • 1
    @xxxvodnikxxx it's good to have anywhere you want to update or use instance variables, just never for static variables. – Stultuske Feb 28 '19 at 14:06

2 Answers2

3

The fact that static variables can be accessed using this is a weird quirk of the Java language. There's really no reason to intentionally do this.

Either use the unqualified name qtdone or use the class name to qualify it: Person.qtdone.

Using this.qtdone works but it looks like it accesses an instance field even when it doesn't. In fact using this syntax doesn't even check if this is in fact referencing an object:

Person notReallyAPerson = null;
notReallyAPerson.qtdone++; // this works!
Michael
  • 41,989
  • 11
  • 82
  • 128
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
2

this.qtdone is equivalent to qtdone or Person.qtdone. However using this for static access is not recommended.

The only difference is, that qtdone might be shadowed by a local variable. In this case, it makes sense to qualify with the class name:

setQtDone(int qtdone) {
  Person.qtdone = qtdone;
}
Kim Kern
  • 54,283
  • 17
  • 197
  • 195