I'm not proposing to change the behavior of working code (except code
that relies on a NoSuchFieldException), so whatever the code does now,
it'll continue to do.
You did not get it. However you define the behaviour of that bit of code,
it runs contrary to intuition.
That's not a problem; I wasn't proposing changing the semantics of the
invokevirtual instruction for somebody to complain that the parallel
using reflection behaves differently from a hardcoded invocation. What
we do need to change is reflexive field access: it should invoke the
getters & setters when the field isn't found. But that's pretty
logical.
[...] I fail to see the logic in your proposal.
Meaning, you'd consider it logical if a reflexive operation behaves
differently from the hardcoded equivalent?
No, that is exactly the point. If i change a public field to a private one
using your method, there still is a declared field that can be retrieved
via Class.getDeclaredFields, but is no longer accessible by reflexive
access. (unless you wish to mix that up as well, and allow access to
private fields if there is a public accessor)
That's precisely the point. Clients should not be allowed to "directly
change the object state". That violates encapsulation. The only thing
Not necessarily. I can imagine Objects that have public fields that should
be accessible. I agree that these are far and few between, but not ever
public field violates encapsulation.
Encapsulation means that _internal_ state must be hidden.
they should be allowed to do is to invoke a method. My proposal
transforms an attempt to do the former into the latter, so you have
the benefits of encapsulation without making an effort to do so, while
preserving compatabaility with existing code, even binary
compatability.
Such a change is incompatible. What if the calling method depends on the
field access semantics? (constant time, guaranteed change)? Not to mention
that reflexive access to a formely public field will now be denied.
I've yet to see a point in it. You keep to iterate the same non-arguments
over
and over again. I'm still waiting for some killer that shows a definite
gain
for all the trouble your proposal introduces.
1)Simpler syntax (you may not agree with this).
It introduces a horrible, impossible-to-untangle confusion what a
dot-operator
does in the case of a field.
2)You don't have to define accessors till you need them.
You always need accessors if you at all rely on the content of the field
for the
internal operation of your class.
3)You don't have to teach novices to make fields private.
You still have to, unless you disallow the public, protected and unmodified
variant of fields (which would kill pretty much all programs)
And that is all i'm going to say about the subject. It may look like a
great idea
to you, but if you really want to have it, use some language that supports
it,
and don't break java over the percieved gain. Java's concepts just do not
mix
too well with it.