[4032] in java-interest
Re: const
daemon@ATHENA.MIT.EDU (Michael Mehrle)
Thu Dec 7 22:38:14 1995
Date: Thu, 7 Dec 1995 16:33:41 -0700
To: "R. Mark Volkmann"<m224873@svmstr01.mdc.com>
From: michael@w3media.com (Michael Mehrle)
Cc: java-interest@java.Eng.Sun.COM
>As far as I can see the C++ "const" keyword is not present in Java.
>In Java, how can you write an accessor function for a class data member
>that returns the data member object but doesn't allow callers to modify it?
>Was this deemed to be an unimportant feature of C++?
>
Mark,
this is not the fact. You *can* define a constant like this:
static final int HEREITIS = 99;
Creating a constant that nobody can meddle with is one way, but consider
the following, which is more elegant (I think ;):
class Safe {
private boolean locked = true;
private int combination = 456;
public boolean isLocked() { // here is one accessor, which returns the
// the status of *locked*
return(locked);
}
public void keyLock(int thisCombination) { // here is another one
if (thisCombination == combination) unLock();
}
private void unLock() {
locked = false;
}
Safe() {}
}
So if you write:
Safe aSafe = new Safe();
you can't access the combination like this:
int = aSafe.combination;
you also can't say:
aSafe.locked = false;
won't work.
.............................
Did this answer your question? I hope, I didn't misunderstand your point.
Michael
\\///
[ o-o ]
____________OOOo___(.)___oOOO_______________
http://www.w3media.com/w3media
michael@w3media.com
Tel. 310.441.9599
Fax 310.441.5919
"One man's mundane and desperate existence
is someone else's Technicolor."
-Strange Days-
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com