Fields
Fields come in two sets: declared on the class, and everything visible through the hierarchy. Each knows its resolved type and reads and writes without access checks.
findField and findDeclaredField
findField searches the whole hierarchy, private fields included; findDeclaredField only the class itself. Both return null when nothing matches.
Which parameters become fields
A val or var parameter always becomes a private field with accessor methods. A plain constructor parameter becomes a field only if the class uses it outside the constructor.
- class A(val x: Int)
- Field x and method x().
- class A(var x: Int)
- Field x, methods x() and x_$eq(int).
- class A(x: Int) { def twice = x * 2 }
- Field x, because twice reads it.
- class A(x: Int) extends B(x)
- No field; x only reaches the super constructor.
The most specific field wins
When a subclass declares a field with the name of a superclass field, fields lists only the subclass's. The hidden ones stay reachable through hidden, and their annotations are merged into annotations.
get and set
Both make the field accessible first, so private fields work without extra steps. A final field can be read but its value should not be written.
Writing a field skips the setter and everything in it. For values that go through validation or events, use a property (bean or annotated) and its setter.