Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Class invariant
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Unchanging property for all objects of a class}} {{about|the computer programming concept|the mathematical concept|Equivalence class|and|Invariant (mathematics)}} {{more citations needed|date=August 2010}} In [[computer programming]], specifically [[object-oriented programming]], a '''class invariant''' (or '''type invariant''') is an [[invariant (computer science)|invariant]] used for constraining [[object (computer science)|objects]] of a [[class (computer science)|class]]. [[Method (computer science)|Methods]] of the class should preserve the invariant. The class invariant constrains the state stored in the object. Class invariants are established during [[Constructor (object-oriented programming)|construction]] and constantly maintained between calls to public methods. Code within functions may break invariants as long as the invariants are restored before a public function ends. With [[Concurrent computing|concurrency]], maintaining the invariant in methods typically requires a critical section to be established by locking the state using a [[mutex]]. An '''object invariant''', or '''representation invariant''', is a computer programming construct consisting of a set of invariant properties that remain uncompromised regardless of the state of the object. This ensures that the object will always meet predefined conditions, and that methods may, therefore, always [[Reference (computer science)|reference]] the object without the risk of making inaccurate presumptions. Defining class invariants can help programmers and testers to catch more bugs during [[software testing]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)