Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 1
    Note that objects aren't the only things with state. If a program uses (mutable) global variables, the program itself is said to have state. Likewise, if a function has a variable that remembers values across function calls, the function is stateful. Commented Apr 10, 2014 at 18:56
  • 2
    @Doval: You can think of global state as the state of a global world object. As far as I know, this view is used e.g. in Ruby. A function that remembers state is isomorphic to an object with just one method. The common basic idea is that you associate values to identities, or places, i.e. that certain things can hold values (possibly mutable value) but retain their identity. Commented Apr 10, 2014 at 19:09
  • 3
    Sure, I agree in principle. I'm just making sure Prog understands that statefulness isn't something exclusive to OOP. I don't think the "everything is an object" line of thought comes naturally. Commented Apr 10, 2014 at 19:24
  • @Doval: You mentioned stateful functions that remember values across different calls. One example I can think of are static local variables in C. Another example is closures (functions that capture variables defined in their context). Closures are somewhat dual to objects: a closure is an object with exactly one method, whereas an object is a collection of closures defined over the same variables. You probably know all this but I wanted to summarize it here. In general, you can store state in some memory location and access it using different mechanisms, as you pointed out. Commented Aug 14, 2015 at 19:11