Const Correct Is Encapsulation

last modified: August 8, 2008

EditHint: Move stuff on this topic from ConstCorrectness to here.

I appreciate ConstCorrectness enormously and miss it sorely in JavaLanguage. :-(

Some languages have RunTime constantness, with freeze and thaw*. Do they allow* thaw when running in a secure mode?

Do freeze and thaw act on the object or the object reference?


Given...

Object nonConst; 
Object const Const; 
Object const & constRef = nonConst; 

nonConst.foo();  //  <-- calls foo() (non-const) if available 
Const.foo();     //  <-- must call foo() const 
constRef.foo();  //  <-- must call foo() const

...note that constancy is a form of encapsulation. Both the Const object and the constRef make their Object more encapsulated - meaning potentially safer and more robust - than the non-constant versions. So "const-correct" code generally extends more usages to constant things than to changeable things.


More literally:

interface FooConst{
        int readX();
        int readY();
},
abstract class Foo implements FooConst{
        abstract public int readX(); 
        abstract public int readY(); 
        abstract public int writeX(int value); 
        abstract public int writeY(int value); 
},

Methods that want a "const Foo" parameter need only declare a FooConst as their argument.


Loading...