Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 7
If you declare a field static, all objects share one copy of the field. When you assign a new value to that field, all objects can see the new value. If
static is not specified, the field is known as an instance field, and each object receives its own copy.
Finally, the value of a field declared transient will not be saved during object serialization. (I explore the topics of transient fields and object serialization in a future
article.)
An instance field is a field declared without the static keyword modifier. Instance fields are associated with objects -- not classes. When modified by an object's code, only the
associated class instance -- the object -- sees the change. An instance field is created when an object is created and destroyed
when its object is destroyed.
The following example demonstrates an instance field:
class SomeClass1
{
int i = 5;
void print ()
{
System.out.println (i);
}
public static void main (String [] args)
{
SomeClass1 sc1 = new SomeClass1 ();
System.out.println (sc1.i);
}
}
SomeClass1 declares an instance field named i and demonstrates two common ways to access that instance field -- from an instance method or from a class method. Both methods
are in the same class as the instance field.
To access an instance field from an instance method in the same class, you only specify the field's name. To access an instance field from another class's instance method, you must have an object reference variable that contains the address of an object created from the class that declares the instance field you want to access. Prefix the object reference variable -- along with the dot operator -- to the instance field's name. (You'll explore instance methods later in this article.)
To access an instance field from a class method in the same class, create an object from the class, assign its reference to an object reference variable, and prefix that variable to the instance field's name. To access an instance field from another class's class method, complete the same steps as when you accessed that field from a class method in the same class. (I'll present class methods later in this article.)
When the JVM creates an object, it allocates memory for each instance field and subsequently zeroes the field's memory, which
establishes an instance field's default value. The way you interpret a default value depends on the field's data type. Interpret
a reference field's default value as null, a numeric field's default value as 0 or 0.0, a Boolean field's default value as false, and a character field's default value as \u0000.
A class field is a field declared with the static keyword modifier. Class fields are associated with classes -- not objects. When modified by a class's code, the class (as
well as any created objects) sees the change. A class field is created when a class is loaded and destroyed if and when a
class is unloaded. (I believe some JVMs unload classes whereas other JVMs do not.)
The example below illustrates a class field: