[java] Const parameter equivalent

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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!!

Share on other sites
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.

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 :)

Share on other sites
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 ) {...}

Share on other sites
Nope, that only prevents you from re-setting the reference but you can change the internals of the object as you want.

Share on other sites
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

Share on other sites
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++

Share on other sites
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 :)

Share on other sites
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-

Share on other sites
'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).

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• Forum Statistics

• Total Topics
633313
• Total Posts
3011318
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!