[java] Const parameter equivalent

Started by
7 comments, last by OrangyTang 18 years, 7 months ago
Hi guys, I'm wondering if Java provides an equivalent of C++'s const parameters, eg: //C++ object class::function(const object& obj) { ... } i.e. I don't want to modify the contents of 'obj' within 'funtion'. Thanks for any help!!
Reject the basic asumption of civialisation especially the importance of material possessions
Advertisement
There is no direct equivalent in Java (it's an RFE but don't expect to see it this decade).

Instead you should pass in a reference to an interface that has only const-like methods on it.

eg. instead of
public void doSomethingWithThis(Vector3f v)

use
public void doSomethingWithThis(ReadableVector3f v)


The other way is to make an immutable copy of the object which implements the immutable interface. A neat trick is to do this on the fly with an anonymous inner class which can be handy if the original object is in a library you can't fiddle with (as is the case with the Java3D Vector3f class):
Vetor3f v;doSomethingWithThis(new ReadableVector3f() {public float getX() { return v.x; }public float getY() { return v.y; }public float getZ() { return v.z; }});


Cas :)
I'm fairly certain that in JDK 1.5 you can use the final keyword to specify that a function parameter is not to be modified.

public void foo( final Bar bar ) {...}
Nope, that only prevents you from re-setting the reference but you can change the internals of the object as you want.
The keyword "const" is already reserved, but it does nothing at all. I also thought that setting a parameter final was equivalent to a constant declaration.

Son Of Cain
a.k.a javabeats at yahoo.ca
Yes, the constant keyword in java is not const, its final.

final int someNum = 1; //const integer, would be const int someNum = 1 in C++
The final keyword is only for primitive types and references, and does not have the same semantics as the const keyword in C++, which as I explained, needs to be engineered into your code sadly.

Cas :)
Also a side note. final as method modifier IIRC semantically means that the method cannot be extended through inheritance, and at the class level that class cannot be extended at all. So if the final were to be used as a modifier of parameters the semantics for methods would have to be changed as well; or maybe just added as a clause like throws.

L-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
'final' in Java works like half of C++'s 'const'. Remember that in C++ you can have a 'const char* const' to specify both the pointer and the contents should be unchangable - in Java 'final' only ever specifies that the reference value itself it unchangable (ie. you can't point it at another object). Unfortunatly this makes it largely unusable for anything other than primative objects.

As stated above, use some kind of immutable interface. The advantage of this is that (unlike C++) someone can't just go casting away the const-ness like you can in C++ (although a particularly dedicated person could still go using reflection to tinker with internals if they really wanted to).

This topic is closed to new replies.

Advertisement