Table of Contents

Java - (Field|Member Variable)

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.

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:

public int cadence;

The public keyword identifies these fields as public members, accessible by any object that can access the class.

Modifiers

Java - Static Modifier

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.

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