** 19.2 What is visibility -- definitions and types
|public||All objects (bad)
|protected||One object and objects derived from it
|Very common. Keep private.
|Static Attribute||=program||One class||Shared by all objects in a class.
|Parameter||one operation call||method||communication between caller and called
|Local||one operation call||one block(C++)/method(Java)||Needs initialization.
|Global||For ever||Any object||Just say NO to global variables.
[ patterns.html#Singleton ]
or a class with a public static getter operation.
Out of the types of visibility (attribute, static attribute, parameter, local, and global) which one is the most commonly used in object-oriented programming?
It is not a matter of popularity! You must not think that you can guess the most popular
visibility for a particular item of data. You must discover the right one.
Worse -- a particular piece of data will have different visibilities to different classes, and
at different times. For example: the Widget class may keep a local "knobs" attributes,
but pass it to another class as a parameter. The receiving class could then store it in a
variable that is local to the method...
But -- as a rule --
the smaller the scope of a variable the less trouble you will have with accidental usage. So,
most experienced programmers will prefer local to parameter, parameter to attribute,
attribute to global. In deed most will reject globally visible data, out of hand.
- Local -- safest
- Static Attribute
- Global -- worst