Talk:Invariant (computer science)

From Wikipedia, the free encyclopedia

Are final/const variables like in C++/Java not also a form of invariants? Wouter Lievens 12:00, 30 Mar 2005 (UTC) In Java, final primatives are indeed invariants. However, final object references are not unless the referenced class is immutable.

[edit] final references

A final reference is an invariant. The object it references may not be. If the object is mutable, the reference can be considered an invariant and the object not an invariant. If the object is immutable, both become invariants. This is an important distinction when a program needs a reference to remain constant, yet needs the data it references to be mutable.

The Object Invariant page seems to be saying the same as the Class Invariant page, but even less coherently. A class invariant is something that is always maintained over the lifetime of every instance of that class. It's enforced by the language's runtime system or sometimes (e.g. in C++) it has to be enforced using various tricks or explicit code. I would just delete it and beef up the article on Class Invariance. Wouter - const (in C++) is a kind of invariance, but class invariance is guaranteed by private data - state that can be guaranteed to be consistent no matter what clients do with the public interface.