Thomas G. Marshall (
[email protected]) wrote:
: Joona I Palaste coughed up:
: > Brian <
[email protected]> scribbled the following:
: >>> The ugliness of this example isn't that the reference is null when
: >>> the variable is accessed; it's that the static variable can be
: >>> accessed through a reference in the first place. Since there's no
: >>> clear meaning to using a static variable through a reference at
: >>> all, it's pointless to talk about whether or not it's consistent to
: >>> allow access through a null reference. At that point, you've just
: >>> gotta abandon "consistent" and read the JLS with a fine-toothed
: >>> comb.
: >
: >> [novice here]
: >
: >> So using static members and methods via references /could/ be
: >> disallowed with no ill effects?
: >
: > Yes. It's just syntactic sugar for accessing them via class names, as
: > only the type of the reference matters, not its value.
: Given that you can do this, again I have no issue with the result. But if
: the question ever became "should you be allowed to grab a class static from
: a reference", then I'd really have to answer "no."
On the other hand, a class should be able to hide implemetation details
about itself. Allowing an object to access a static item in its class
allows the implementation to be changed. In particular, something that is
initially implemented as a class constant might later become something
that varies based on other factors within each object. E.g. sales tax on
every item might be a constant when the code is written, but the next year
this might be changed to vary based on the cost of an item. The tax
"constant" is no longer a constant, but any code that said "item.tax_rate"
would still work.
Of course you may wish to have implemeted a getTaxRate method in the first
place, but that's a different issue.