Fields and Variables


In the class
CelestialBody radius, name and mass are examples of fields/variables. Every object of the type CelestialBody has its own instances of these fields. Changing the mass field in one object will not affect the mass in another object.



1 Duplicate Field Names

Duplicate field names is not allowed in class declarations. The name of a declared element must be different from the names of all other declared elements in the class.



Only the syntax is Ok, but not the semantics, i.e., typing and declarations have not yet been checked.


1.1 Simulation of IllegalDuplicate to Perform Checking

As we can see when simulating IllegalDuplicate, the class generates an error for trying to redefine the field duplicate.



2 Identical Field Names and Type Names

2.1 Voltage


2.2 IllegalTypeAsField

It is not allowed to have the same name of a field/variable and its type specifier.



2.3 Checking of IllegalTypeAsField

Semantic checking is also performed during flattening. As we can see when simulating IllegalTypeAsField we get a warning for trying to name our variable Voltage, which is the same as the type name.



2.4 Fixing the Error

By changing the name of the first field/variable to voltage1, we fix the problem.