Static and non static fields

前端 未结 6 882
有刺的猬
有刺的猬 2020-12-04 00:36

just to clarify I am thinking of this right, in java a static field is a variable/field which is used by a whole class, or can be used by all objects refering to that class?

6条回答
  •  北海茫月
    2020-12-04 01:12

    • static field shared and used by all the objects and loaded when class is loaded
    • non static fields are separate copies for every object and loaded when an object is created

    And a non static field is a variable defined by an object?

    Whenever you create a new objects, each object will have its own copy of instance i.e. non static fields

    And a second object refering to the same class as object 1 can have a different value to object 1's static field?

    Didn't really get your question, but

    • If object1 and object2 are instnaces of a class, then if object1 modifies static field of class, then object2 will get the updated value

提交回复
热议问题