A mode system for read-only references in Java

The use of references is typical in object-oriented programming. They are used for e.g. constructing compound objects, i.e. objects that hold references to other objects, and to perform in-place updates. Object sharing with references introduces problems since every object holding a reference to another can use it to invoke methods that modify the referenced object’s state. This makes it harder to control the origin of changes to a specific object and thus makes it harder to reason about programs [8]. Since all references always give full access to their referenced objects’ protocol, it is generally not possible to share an object by reference without risking that its mutator methods are used to modify it. In C++, this problem can be partly addressed by the use of const objects, const pointers and const methods although with some limitations. This is not possible in Java since it lacks a similar construct. Instead, approaches such as the proxy-pattern or more Java-specific constructs such as interfaces can be used to some extent but with some limitations, see e.g. [7] and [8]. We propose a new Java construct for read-only references that protects the transitive state of its referenced object. It is an improvement over C++’s const in that it is transitive and furthermore it does not suffer from some of the limitations of e.g. protection through interfaces or the limitations of the read-only constructs reviewed in related work.

[1]  Jan Vitek,et al.  Flexible Alias Protection , 1998, ECOOP.

[2]  Matthias Felleisen,et al.  A Programmer's Reduction Semantics for Classes and Mixins , 1999, Formal Syntax and Semantics of Java.

[3]  John Hogg Islands: aliasing protection in object-oriented languages , 1991, OOPSLA 1991.