Re: How do java programmers cope with java missing c++ const?
- From: "Michael Redlich" <mike@xxxxxxxxxxx>
- Date: 28 Feb 2006 09:06:24 -0800
josh.s17@xxxxxxxxx wrote:
I've been working with java for a while now, but I miss the ability to
pass around objects that can't be modified, as is possible with the c++
const keyword. Is it just me or do others find this a limitation of
java? I've read the way to do it in java is to pass around immutable
objects however that seems a lot less flexible than having the ability
to pass a const reference to a mutable object as can be done in c++.
Josh:
I also found the 'const' keyword in C++ to be very useful, especially
for member functions and overloaded operators. I always thought that
Java could have benefited from this mechanism as well.
However, as Opalinski pointed out, there was potential for problems
when dealing with someone else's code where the developer didn't fully
understand how/when to use the 'const' keyword. I suppose that I was
never burned with that.
In any event, you could do similar things in Java using the 'final'
keyword. It is a modifier used to change the characteristics to
classes, methods, field, and parameters so that those items cannot be
changed.
The following tips were taken from my Java reference book:
* A class that is declared 'final' cannot be subclassed or declared to
be abstract.
* A method that is declared 'final' cannot be overridden or declared to
be abstract.
* A field (class or instance) that is declared 'final' cannot be
modified and must be initialized at the point of declaration.
* The 'final' reserved word can be applied to formal parameters to
prevent the parameter's value from changing.
* A constructor cannot be modified with the 'final' reserved keyword.
Hope this helps...
Mike.
-----
ACGNJ Java Users Group
http://www.javasig.org/
.
- Follow-Ups:
- Re: How do java programmers cope with java missing c++ const?
- From: Thomas Hawtin
- Re: How do java programmers cope with java missing c++ const?
- From: Dimitri Maziuk
- Re: How do java programmers cope with java missing c++ const?
- References:
- How do java programmers cope with java missing c++ const?
- From: josh . s17
- How do java programmers cope with java missing c++ const?
- Prev by Date: Re: javap issue
- Next by Date: Re: Round Robin scheduling
- Previous by thread: Re: How do java programmers cope with java missing c++ const?
- Next by thread: Re: How do java programmers cope with java missing c++ const?
- Index(es):
Relevant Pages
|