Fields are members that represent variables. A field-declaration introduces one or more fields of a given type.
A field-declaration may include set of attributes (§17), a new
modifier (§10.2.2), a valid combination of the four access modifiers (§10.2.3), a static
modifier (§10.4.1), and a readonly
modifier (§10.4.2). The attributes and modifiers apply to all of the members declared by the field-declaration.
The type of a field-declaration specifies the type of the members introduced by the declaration. The type is followed by a list of variable-declarators, each of which introduces a new member. A variable-declarator consists of an identifier that names the member, optionally followed by an "=
" token and a variable-initializer (§10.4.4) that gives the initial value of the member.
The type of a field must be at least as accessible as the field itself (§3.3.4).
The value of a field is obtained in an expression using a simple-name (§7.5.2) or a member-access (§7.5.4). The value of a field is modified using an assignment (§7.13).
A field declaration that declares multiple fields is equivalent to multiple declarations of single fields with the same attributes, modifiers, and type. For example
class A { public static int X = 1, Y, Z = 100; }
is equivalent to
class A { public static int X = 1; public static int Y; public static int Z = 100; }