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.