Accessing Static variables

后端 未结 8 1098
清歌不尽
清歌不尽 2020-12-31 09:50
public class Bicycle {

    private int cadence;
    private int gear;
    private int speed;
    private int id;
    private static int numberOfBicycles = 0;

    p         


        
8条回答
  •  遥遥无期
    2020-12-31 10:42

    May be what your lecturer said is regarding accessing them from outside the class not from inside the class. static variables can be accessed outside the class like this ClassName.VariableName or object.VariableName. But however the first method is preferrable.

    From inside the class it's not needed you may use this keyword or classname-qualifier to disambiguate with the local variables with the same name inside methods and constructors.

提交回复
热议问题