Accessing Static variables

Static variables are owned by class rather than by its individual instances (objects). Referring static variables outside the class is by ClassName.myStaticVariable but inside the class it is similar to other instance variables.

You can always use static variables in non-static methods but you cannot use non-static variables in static methods reason being when static methods are loaded other non-static instance variables are not created.

So your statement id = ++numberOfBicycles; is perfectly valid and will compile without errors.


From within the class the Bicycle qualifier is optional on static variables, just like the this qualifier is optional on instance variables

Tags:

Java