Understand the modifier static
. A definition would be that when we declare a variable as Static, it becomes an attribute of the class and not of the instance. We can also think of this variable as a global attribute of a class.
Take an example:
Class with 2 attributes one static and one non-static.
public class ClasseComAtributoStatic {
public static int contadorStatic;
public int contadorInstacia;
public ClasseComAtributoStatic() {
contadorStatic++;
contadorInstacia++;
}
}
Test class.
public class TesteStatic {
public static void main(String[] args) {
ClasseComAtributoStatic classeComAtributoStatic = new ClasseComAtributoStatic();
System.out.println("1- Atributo static (contadorStatic): " + classeComAtributoStatic.contadorStatic);
System.out.println("2- Atributo instancia (contadorInstacia): " + classeComAtributoStatic.contadorInstacia);
ClasseComAtributoStatic classeComAtributoStatic2 = new ClasseComAtributoStatic();
System.out.println("3- Atributo static (contadorStatic): " + classeComAtributoStatic2.contadorStatic);
System.out.println("4- Atributo instancia (contadorInstacia): " + classeComAtributoStatic2.contadorInstacia);
ClasseComAtributoStatic classeComAtributoStatic3 = new ClasseComAtributoStatic();
System.out.println("3- Atributo static (contadorStatic): " + classeComAtributoStatic3.contadorStatic);
System.out.println("4- Atributo instancia (contadorInstacia): " + classeComAtributoStatic3.contadorInstacia);
System.out.println("Reimprimindo atributo static (contadorStatic) da primeira isntacia: " + classeComAtributoStatic.contadorStatic);
}
}
Result of the execution of the Testestatic class
1- Atributo static (contadorStatic): 1
2- Atributo instancia (contadorInstacia): 1
3- Atributo static (contadorStatic): 2
4- Atributo instancia (contadorInstacia): 1
3- Atributo static (contadorStatic): 3
4- Atributo instancia (contadorInstacia): 1
Reimprimindo atributo static (contadorStatic) da primeira isntacia: 3
Let’s understand what happened here. I want to know how many times my class ClasseComAtributoStatic
was instantiated by the builder I created. Knowing which static attribute belongs to the class and not to each instance, every time the constructor executes the static attribute contadorStatic
is incremented and "keeps" its value until the end of execution. The attribute contadorInstacia
is also incremented but it belongs to each instance of the object, so its value is always in 1. That is, comparing the two attributes in the result of the TesteStatic
we will see that the contadorStatic
increments and "stores" the amount of times the constructor was invoked.
I strongly recommend that you create and test these two classes to better understand how a Static attribute works. I also recommend reading and exercises of this link
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero