2.10 Static Types and Dynamic Types2.9 Object Assignments2 Java with Caffeine2.11 Generic Methods

2.10 Static Types and Dynamic Types

As demonstrated above, an object variable has two kinds of types:

  1. A  static type: this is the type with which the variable has been declared. It determines to which fields and methods of the variable we can refer.

    In above example, the static type of variable a is Article.

  2. A  dynamic type: this is the type of the object to which the variable currently refers. The dynamic type can change with every assignment, but it always is a subtype of the static type. It determines which methods are actually called.

    In above example, the dynamic type of variable a is Book.

We call object variables  polymorph because they can refer to objects of different types (that are subtypes of a common supertype).


© Wolfgang Schreiner; February 3, 2005

2.10 Static Types and Dynamic Types2.9 Object Assignments2 Java with Caffeine2.11 Generic Methods