I googled the topic, but besides Wikipedia I didn't find any further useful documentation or articles.
Can anybody explain to me in simple words what it means or refer me to some nice and easy to understand documentation?
I googled the topic, but besides Wikipedia I didn't find any further useful documentation or articles.
Can anybody explain to me in simple words what it means or refer me to some nice and easy to understand documentation?
They are facts that must be true about an instance class. For example if a class has a property X and invariant can be X must be greater then 0. To my knowledge there is no built-in method for maintaining invariants you must make properties private and make sure your getters and setters enforce invariance property.
There are annotations available which can check properties using reflection and interceptors. http://docs.oracle.com/javaee/7/api/javax/validation/constraints/package-summary.html
Invariant means something that should stick to its conditions no matter whatever changes or whoever uses/transforms it. That is to say, a property of a class always fulfills or satisfies some condition even after going through transformations by using public methods. So, the client or user of this class is ensured about the class and its property.
For example,
It doesn't mean anything in particular in reference to Java.
A class invariant is simply a property that holds for all instances of a class, always, no matter what other code does.
For example,
X has the class invariant that there is a
y
property and it is nevernull
and it has a value of typeY
.This fails to maintain two important invariants:
count
never returns a negative value because of possible underflow.count
are strictly monotonically increasing.The modified class preserves those two invariants.
...but fails to preserve the invariant that calls to
count
always succeed normally (absent TCB-violations†) becausecount
might throw an exception or it might block if a deadlocked thread owns the counter's monitor.Each language with classes make it easy to maintain some class invariants but not others. Java is no exception:
private
fields, so invariants that rely on private data are easy to maintain.null
values to sneak in in many ways, so it is tough to maintain "has a real value" invariants.† - An externality or TCB violation is an event which a systems designer optimistically assumes will not happen.
Typically we just trust that the basic hardware works as advertised when talking about properties of high-level languages built on them, and our arguments that invariants hold don't take into account the possibility of:
setAccessible
to modifyprivate
lookup tables.For some systems our TCB might include only parts of the system, so we might not assume that
...but we might assume that:
The higher-level a system, the larger its TCB typically is, but the more unreliable things you can get out of your TCB, the more likely your invariants are to hold, and the more reliable your system will be in the long run.