| Thread overview | |||||
|---|---|---|---|---|---|
|
January 02, 2006 final class member variable NOT LIKE JAVA! | ||||
|---|---|---|---|---|
| ||||
Expecting final to work as it does in Java, I wrote the following:
class Bar {
int i = 2;
}
class Foo {
final Bar bar;
this(Bar bar) { this.bar = bar; }
}
You can imagine my surprise when I was allowed to do the following:
int main(char[][] args) {
Foo foo = new Foo;
foo.bar = new Bar; // final should not allow this!
return 0;
}
I worked around it by creating a property method that only permits 'get' access to bar, but it was so much more elegant when it was just final Bar bar.
Oh well,
Garett
| ||||
January 02, 2006 Re: final class member variable NOT LIKE JAVA! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | See the in digitalmars.D.learn ng. There was this "final variable" discussion just yesterday.
Frank
--
D goes real-time: http://www.drealtime.com
| |||
January 02, 2006 Re: final class member variable NOT LIKE JAVA! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | On Sun, 01 Jan 2006 21:06:57 -0500, Garett Bass <garettbass@studiotekne.com> wrote:
> Expecting final to work as it does in Java, I wrote the following:
>
> class Bar {
> int i = 2;
> }
>
> class Foo {
> final Bar bar;
>
> this(Bar bar) { this.bar = bar; }
> }
>
> You can imagine my surprise when I was allowed to do the following:
>
> int main(char[][] args) {
> Foo foo = new Foo;
>
> foo.bar = new Bar; // final should not allow this!
>
> return 0;
> }
>
> I worked around it by creating a property method that only permits 'get'
> access to bar, but it was so much more elegant when it was just final Bar
> bar.
>
> Oh well,
> Garett
>
Use const instead.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply