CKTIS / Java
Field access using the dot
The notation Type.FIELD / object.member distinguishes static field access through a type name from object field access. Field selection depends on the compile-time type, and instance field access requires a non-null reference.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
Type.FIELD
object.member
this.member
super.memberIn Type.FIELD / object.member, the slash separates two alternatives; it is not part of the code. Type.FIELD identifies a static field, while object.member can identify an instance field. These names are placeholders. The field must exist and be accessible; the dot does not bypass access control. Formally, Type.FIELD is a qualified name.
this.member can select an instance field of the current object or a static field, which belongs to the class, not to the current object. super.member can refer to a superclass field hidden in a subclass. Field selection depends on the expression's compile-time type, not the object's runtime class.
Accessing an instance field through null throws NullPointerException. A static field can be read through an object expression even when its value is null: the expression is evaluated, but its value is discarded. Prefer the type name to make static access clear.
In the example, Base.LIMIT reads a static field. object.value selects the field from Base, whereas child.value selects the field from Child, although both variables reference the same object.
Examples
public class Main {
static class Base {
static final int LIMIT = 10;
int value = 1;
}
static class Child extends Base {
int value = 2;
}
public static void main(String[] args) {
Child child = new Child();
Base object = child;
System.out.println(Base.LIMIT);
System.out.println(object.value);
System.out.println(child.value);
}
}Result
10 1 2
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.