What is the difference between referencing a field by class and calling a field by object?

前端 未结 5 1843
渐次进展
渐次进展 2021-01-26 01:56

I have noticed that there are times when coding in Java that I have seen fields called by method:

System.out.println(object.field);

and

5条回答
  •  暖寄归人
    2021-01-26 02:30

    object.field should be (see note below) an instance member while Class.field would be a static member.

    Note: Like stated by @radai and I think it's worth mentionning, you can also access a static member through an object instance, however that's a very bad practice which is quite misleading.

提交回复
热议问题