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

前端 未结 5 1994
渐次进展
渐次进展 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:20

    The field Class.field can be accessed without creating an instance of the class. These are static fields which are initialized when the classes are loaded by classloaders.

    The other field i.e. object.field can be accessed only when an instance of the class is created. These are instance field initialized when object of the class is created by calling its constructor.

提交回复
热议问题