What is the proper way of accessing static fields in Java?

后端 未结 6 1488
面向向阳花
面向向阳花 2020-12-04 00:24

I just started learning Java and I wrote a class to test using static fields. Everything works fine but in Eclipse I see an icon which when hovered comes out as: \"The stati

6条回答
  •  清歌不尽
    2020-12-04 01:00

    Static fields and methods are not belong to a specific object, but to a class, so you should access them from the class, and not from an object:

    CarCounter.getCounter()
    

    and not

    a.getCounter()
    

提交回复
热议问题