Chris London
Chris London

Reputation: 81

Static and non static fields

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? And a non static field is a variable defined by an object? And a second object refering to the same class as object 1 can have a different value to object 1's static field?

Upvotes: 2

Views: 10998

Answers (6)

Óscar López
Óscar López

Reputation: 235984

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).

Upvotes: 1

arshajii
arshajii

Reputation: 129477

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.

Upvotes: 0

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

  • 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

Upvotes: 1

Aubin
Aubin

Reputation: 14853

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).

Upvotes: 0

hd1
hd1

Reputation: 34657

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.

Upvotes: 0

blackpanther
blackpanther

Reputation: 11486

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.

Upvotes: 5

Related Questions