I have been reading Java tutorials from the very beginning, and I have a question about the <> t20> key for fields or variables. As Javasaid here :
Class Variables (Static Fields)A class variable is any field declared using a static modifier; this tells the compiler that there is only one copy of this variable, no matter how many times the class instance has been created. The field that defines the number of gears for a particular type of bike can be marked as static, since conceptually the same number of gears will be applied to all instances.
With this, I assume that if you have an object (in this case an instance of the class Bicycle) and a field inside it, then it, staticthen, regardless of if you confirm bicycle1or bicycle2, the field in which its static value will have the same value. Am I mistaken, or do I understand this well?
I mean, if I have:
Bicycle bicycle1 = new Bicycle();
Bicycle bicycle2 = new Bicycle();
and in the class BicycleI have a field static, for example:
class Bicycle{
static int gears;
}
And in bicycle1I set the gear value to seven:
bicycle1.setGears(7);
then if I try to get the gears value in bicycle2, I have to get the same value as on bicycle1, right?
System.out.println(bicycle2.getGears());
Well, that’s where I doubt it because I Javasaid in the quote I put above:
this tells the compiler that there is only one copy of this variable
? ? ?