Re: final and const java help needed for an old C++ programmer



apm35@xxxxxxxxxxxxxxxxxx wrote:
Hello,

I am relatively new to java but have been using C++ for ages. In C++ I
try to write code that is const-correct but I do not know how to do
the same in java. Can anyone advise please? When I looked at this
initially it seemed like final might do what I want but that only
seems to work for fundamental types. When the argument is an object
reference final just stops the reference being changed. You can still
alter the state of the object that the reference refers to. Also I
notice that final does not seem to get used much. I wonder why that
is....

final != const

Not even "They differ in some subtle way known only by
language lawyers," but "They are not in any way alike." You
will only confuse yourself further if you try to understand
final as a variation on const. To put it another way,

Java != C++

.... and you will only get into trouble by trying to understand
one as a dialect of the other. Learn each language on its own
terms, with its own strengths and weaknesses, and you'll be much
better off.

On a variable declaration, final means "This variable cannot
be changed after initialization," and for member variables it
also means "... and initialization must occur during construction
or class loading." If you want an object whose state does not
change, shield its members from outside influence and don't write
mutator methods.

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: How java passes object references?
    ... Which everybody knows, it doesn't in Java. ... The reason I think this is a useful clarification is that when you got to the part about how passing by reference might work, it seems you went off track at least partly because you didn't understand the nature of the above. ... is a pointer pointing at the memory block. ... Assignments to local variables, or even to class members, do not allocate memory. ...
    (comp.lang.java.programmer)
  • Re: The Java no pointer big fat lie!
    ... > reference types, it wouldn't be a good comparison. ... and what you can't - and there is a big difference in Java to C and C++. ... You cannot change it in the language itself, ... Thus I would distinguish pointers from ...
    (comp.lang.java.programmer)
  • Re: call by reference
    ... We started learning Java about one month ago, and they just don't believe me when i try to explain that there is a confusion when passing reference types as an argumetn to a function. ... The actual parameter must be an L-value. ...
    (comp.lang.java.programmer)
  • Re: programming concepts > specific languages
    ... >actually changes the target of the reference passed to 'function2'. ... Java from a source that was not precise about terminology. ... if I have a class MyObject and write ... I thought primitive types in particular behave in the ...
    (comp.programming)
  • Re: no pointer in Java => my problem
    ... Does Java pass objects by reference or by value? ... f could also modify the StringBuffer by appending " World" for instance. ...
    (comp.lang.java.programmer)