Re: How do java programmers cope with java missing c++ const?




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/

.



Relevant Pages

  • Re: Statement functions
    ... It would be a simple way to avoid ambiguity with ... array assignment, especially in the case of an internal subprogram. ... same name and you forget to give a local type declaration ... As for the keyword STMT, I'm sure that STATEMENT is a better Fortran-istic choice. ...
    (comp.lang.fortran)
  • Re: Static Read-only array fields in C++
    ... this appears only to be a problem relating to managed ... the 'const' keyword in the ... > If not using arrays, does the 'const' keyword actually result in the ...
    (microsoft.public.dotnet.languages.vc)
  • Re: [announcement] SYSAPI and SYSSVC for Windows
    ... use dangling pointers -- so the & operator in C or C++ is more like ... > the array declaration. ... if declared so or if allocated in the heap by ... We should, instead, have allowed the "aliased" keyword on parameters. ...
    (comp.lang.ada)
  • Re: Why is the kfree() argument const?
    ... "restrict" is the C99 keyword for such requirement (or better ... The restrict keyword controls aliasing, ... inserting const there would do anything at all. ... that a function taking a const pointer argument will not modify the memory ...
    (Linux-Kernel)
  • Re: List<> of struct with property. Cannot change value of property. why?
    ... I was under the impression that we were discussing the "const" keyword as it applies to function declarations. ... Second, in the above example, the second line of code doesn't compile. ... In other words, any difference between the two lines of code you posted are caused *not* by the use of the "const" keyword, but rather by the differences in the way the compiler deals with a pointer versus an array. ...
    (microsoft.public.dotnet.languages.csharp)