Object-Oriented Variables – What is Internal State and Difference Between State and Internal State

definitionobject-orientedstatevariables

Ok, this question has been asked before, but it's still not clear to me. What exactly is internal state?

So far state is clear to me:
Functions contain behavior, variables have state, values don't. So state typically means the current value or data something like variables contain, simply said, if I'm right. By changing their values, you change their state (if this isn't correct, let me know :)).

But internal state is not very clear to me, it almost seems to me people are mixing up classes and objects: https://stackoverflow.com/questions/21345975/what-is-internal-state

And this definition isn't 100% clear te me either: http://www.cs.nmsu.edu/~rth/cs/cs177/map/intstate.html

So Objects contain internal state, am I right? So the object's state is actually the internal state? So If I have a class:

class Foo { 
    public $aMemberVar = 'aMemberVar Member Variable'; 
    public $aFuncName = 'aMemberFunc'; 


    function aMemberFunc() { 
        print 'Inside `aMemberFunc()`'; 
    } 
} 

And I create an object:

$foo = new Foo; 

The objects internal state consists of the values $aMemberVar and $aFuncName ?? And the internal state can change, for example by changing:

$foo->aMemberVar = 'new value';

But to be clear, the internal state includes private variables as well? You cannot change them, but they are part of the internal state?

Do I understand this correctly or do I misunderstand something? I'm quite confused cause why should you name it internal state if it's typically just state…

Best Answer

Internal state mainly makes sense as a term to differentiate from externally visible (and possibly directly modifiable) state.

Otherwise state is state. Don't worry about saying internal unless you need to differentiate from external. If you just say "state", it implies all state information unless you make it more specific through context.

Linux has a lot of internal state that it does not share with users. But you can see some external representation of state in tools like top or ps. Trying for a useful analogy.

Yes private members (variables, properties depends on the language) are part of state, as long as the object exists. They are internal, of course.

There's a whole other way to use the term external state, to represent the state of the environment in which the object or system exists. Some people use it this way. Other people just mean object state that is public.

Related Topic