Attributes on a class are decalred at the start of the class body.
These declarations take the form of:
[FieldModifiers] FieldType Identifier [= InitialValue];
where
-
FieldModifers - These are optional and are used to supply addtional informtion
to the compiler. The valid values are:
-
public - This attribute is accesable from the calss and any other
class.
-
protected - This attribute is only accessable from the class and
any of it's sub-classes.
-
private - This attribute is only accessable by the class itsself.
-
final - If an attribute is declared as final an initial value must
be supplied for it. This value then becomes the permanent value for the
attribute and it cannot be changed.
-
static - There is only ever one instance of a static attribute.
This instance lives on the class. This instance of the attribute is shared
by all the objects of the class.
-
transient - This is used to indicate that the attribute is not part
of the persistent state of an object. If the object is saved to a persistant
store then this attribute is not saved.
-
volatile - This is used to indicate that the value of the attribute
must be checked against the master copy when used by threads. Threads typically
only access a copy of the attribute for effecency reasons.
-
FieldType - This is the type of attribute. ie int, String, byte, etc.
-
Identifier - This is the name of the attribute.
-
InitialValue - An attribute can be supplied with and initial value.
Here is an example attribute definitions:
public class HoldValues
{
protected int x;
public int y;
private String myString;
final int MAX_VALUE = 5;
static long counter = 0;
}
Sources