Newsgroups: comp.lang.java,comp.object,comp.object.logic
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!swrinde!howland.reston.ans.net!newsfeed.internetmci.com!EU.net!CERN.ch!dxnews.cern.ch!kruse
From: kruse@cms6.cern.ch (Andres Kruse)
Subject: Java public instance variables harmful?
X-Nntp-Posting-Host: cms6.cern.ch
Message-ID: <KRUSE.96Mar6131047@cms6.cern.ch>
Sender: news@news.cern.ch (USENET News System)
Organization: CERN European Lab for Particle Physics
Date: Wed, 6 Mar 1996 12:10:47 GMT
Lines: 72
Xref: glinda.oz.cs.cmu.edu comp.lang.java:29326 comp.object:45394 comp.object.logic:714


Hi Everyone,

this is a question concerning the design of the Java language,
I asked a similar yesterday with overwhelming (none) response
in comp.lang.java.. so here I go again:

I feel that there is a missing feature in the Java language design,
but maybe it's just me who doesn't understand it correctly, maybe
it's already there..

  The fact that instance variables can be made public seems very
dangerous and IMHO it's clearly against the idea of encapsulation.
  But, that wouldn't be such a big problem if there was a way to
sort of fake a public instance variable through an access method.

So what I'm asking is wether there is a way to have the following:

A class Point() has made public its two instance variables x and y.
Later when you are using the Point() class all over the place you suddenly
realize that x and y are actually not the best variables. You would 
rather like to have different instance variables, say in a coordinate
system of radius and angle r and phi.

But since you have made x and y public you are bound to stick to 
that design for ever unless there is a way to secretly replace the
public instance variable by access methods. This should be done in
such a way that none of the method that accesses these instance
variables notices the change.

So it would require that you provide two access methods, one that
"stores" and one that "reads" the variable. The appropriate access
method has to be chosen depending on the context, I guess (wether
it is on the left or right side of '=' ?) In the case above 
"storing" would require a couple of mathematical transformations
just like "reading" but that doesn't matter so much.

I think I can give for the above example a good reason why you
might want to change it. Suppose you have an application that uses
Points always in an environment that requires access to 'r' and 'phi'.
Obviously you would make an access method to the class Point() that
returns these quantities calculated from the instance variables 
'x' and 'y'. Later you realize that you could speed up the whole thing
considerably if you would only have decided in the beginning to use
'r' and 'phi' immedidately as instance variables... but now it's 
too late... your code is developed and slow...

And this is not due to bad design because it might be that during
the life cycle of your software more emphasis is put on certain
applications, hardware gets faster etc.. I mean, things which you
could not forsee in the design.

My question is inspired by the idea that even access to the 
public instance variables should be seen as a part of the interface to an 
object. This is a very special service that the object provides but 
it's nothing but a service.

So, my question: Is the above possible in Java and I just don't see how?
Is it at all desirable to have a mechanism like that?
Should one simply "never make instance variables public"?

I remember hearing once that Eiffel has a mechanism for something
of that kind.

Thanks for reading up to this point and your comments,

      Andres

-- 
-------------------------------------------------------
Andres Kruse, kruse@cern.ch | CERN Geneva, ECP division
-------------------------------------------------------
