George Elkins said:
An object's state can encapsulated (hidden) simply by using private data
members and public methods to change the object's state. It's much more
powerful and safe than C's local static variables.
why "much more powerful and safe" ?
i do not think that it is much more powerful and safe than local
static variable.
the reasons is as follows:
first, let define the following terms.
1) 'pure' means 'do not have any local variable'.
2) 'stateful' means 'maintain a value during
applicatin run-time'.
in java,
methods of a class are not 'pure' ones.
Now, let consider about 'stateful'.
in programming,
i think that 'stateful' object(class) is important and
is used frequently and commonly.
therefore, a language have to give us simple/easy syntax
for 'stateful' implementation
local static variable VS private static data member.
this is a choice/problem of "how making 'stateful' ?".
in java, (as i know as)
local static variables of a method is not allowed.
therefore,
it will result in the following codes for static object:
===============================================
Public Class StateTest {
..
private static int mStateful = ..;
..
public StatefulMethod ( ... ) {
..
.. = mStateful .. ;
..
}
}
===============================================
but, in this codes,
'mStateful' is _not_ local variable of 'StatefulMethod'.
that is, other methods of 'StateTest' can access it, too.
it is not safe if it is required that only 'StatefulMethod'
have to access it.
moreover,
as the number of static data member increases,
the readability of source decreases and the confusion between
static data members increases.
i think that the following codes will match with
"encapsulated (hidden)" conception.
===================================================
Public Class StateTest {
..
public StatefulMethod ( ... ) {
static int mStateful = ..;
..
}
}
====================================================
and, as i know as,
almost IDEs support variable list.