Static and non static fields

前端 未结 6 872
有刺的猬
有刺的猬 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:01

    A static field, or static class variable within a class is accessible before an instance of that class is created unlike instance variables. Instance variables (non-static variables) within a class are created when an instance of that class is created at run-time. Hence, non-static variables cannot be accessed until an instance of that class is created. Whereas, static class members can be accessed before that class is created or instantiated.

    All instances of that class can access the same static variable. On the other hand, instance variables are individual/encapsulated to each instance of a class.

    0 讨论(0)
  • 2020-12-04 01:10

    Kind of... a static object is shared between instances of a class and a non-static is specific to the instance. Same goes for methods.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-12-04 01:15

    An instance attribute is one that is specific to an instance, and its value isn't shared among other instances of the same class.

    On the other hand, a class (or static) attribute is one that is common to all of the class' instances, as it belongs to the class, not to an instance in particular.

    So you must be careful with the static attributes, because a change in one will be reflected on all of the code that uses it, sometimes causing unexpected results. In practice, I tend to avoid static attributes, except for the cases where they have constant, immutable values.

    Similar considerations apply to instance methods and static methods: an instance method can "see" both instance and static methods and attributes, whereas a static method can only refer to static methods and attributes of the class, and can't "see" the instance methods and attributes (that is unless it instantiates an object and uses it to access its instance members).

    0 讨论(0)
  • 2020-12-04 01:15

    Refer to JLS §8.3.1.1:

    If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4).

    By contrast, each instance of a class contains its own unique values for non-static fields. Non-static fields are incarnated when the class is instantiated:

    A field that is not declared static (sometimes called a non-static field) is called an instance variable. Whenever a new instance of a class is created (§12.5), a new variable associated with that instance is created for every instance variable declared in that class or any of its superclasses.

    0 讨论(0)
  • 2020-12-04 01:16

    As said in the reference :

    If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4).

    0 讨论(0)
提交回复
热议问题