About
A field is a variables in a class and they are used to contain state information. They are also known as member variable.
A member that is not declared as static is implicitly an instance member.
Articles Related
Field
Initialization
Fields are initialized when class is loaded into memory. They are initialized from top to bottom in the order they are declared in Java source file.
Declaration
Field declarations are composed of three components, in order:
- 1. Zero or more modifiers, such as public or private.
- 2. The field's type (datatype).
- 3. The field's name.
public int cadence;
The public keyword identifies these fields as public members, accessible by any object that can access the class.
Modifiers
Access
The first (left-most) access modifier used lets you control what other classes have access to a member field. For the moment, consider only public and private.
- public modifier—the field is accessible from all classes.
- private modifier—the field is accessible only within its own class.
In the spirit of encapsulation, it is common to make fields private. This means that they can only be directly accessed from their owne class. We still need access to these values, however. This can be done indirectly by adding public methods that obtain the field values