Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const function arguments in java?

Is there a way to achive something similar to C++'s const in Java? Specifically, I have a function like

private static Vector2 sum(Vector2 vec1, Vector2 vec2) {
    return vec1.cpy().add(vec2);
}

and I want to

  1. make clear in the signature that it doesn't modify it's arguments,
    and
  2. enforce that it doesn't modify it's arguments (preferably at compile time, but inserting runtime assertions would also be OK).

Now I know that java is strictly pass-by-reference (I'm just teasing, I know it is pass-by-value or rather pass-by-copying-a-reference of course). What I mean is that in Java, when you call a method, the reference is copied, but that reference points to the same object contents. If a class has public fields or setters, a called method can always modify the contents of a passed object. Is there any e.g. annotation like @NotNull or tool to prevent this? I just found the JetBrains annotations like @Contract(pure = true), but I don't think they provide any checking.

like image 254
jdm Avatar asked Dec 28 '16 11:12

jdm


People also ask

How do you pass a constant argument in Java?

To make any variable a constant, we must use 'static' and 'final' modifiers in the following manner: Syntax to assign a constant value in java: static final datatype identifier_name = constant; The static modifier causes the variable to be available without an instance of it's defining class being loaded.

What is const function parameter?

Declaring function parameters const indicates that the function promises not to change these values. In C, function arguments are passed by value rather than by reference. Although a function may change the values passed in, these changed values are discarded once the function returns.

Does Java have const functions?

Constants are basically variables whose value can't change. In C/C++, the keyword const is used to declare these constant variables. In Java, you use the keyword final .


2 Answers

You can not guarantee that method won't change the parameters. If you want to avoid changing the object, you should make it immutable. You can use some wrapper classes to be passed inside, without provided setters. Or you can make your setters package-local and use some access helper classes in the same package if you need to call some package-local method.

like image 160
Orest Savchak Avatar answered Sep 18 '22 14:09

Orest Savchak


You can add final to the parameter, but this will only prevent a initialisation of those, you still can call method and setter modifying the content of your Vector.

If you need the restrict the access to those, you might need to create a immutable class hiding the Vector, a wrapper. Basicly it will redirect only the methods that prevent any update by hiding the setter and limit the getter to primitive values, returning an instance give a possibility to change a value in it.

Of course, there is also some drastic solution, you could clone the Vector and his content. Keeping the instances safe even if someone try to update some values. This will only be a problem during this call, using wrong values but will keep the original instances unchanged.

Or you could use both solution, creating a wrapper that return cloned instance (just need to provide a get(int index) that return a clone). This solution is a compromise between memory consumption (cloning only needed instance) and restrictive getter.

like image 29
AxelH Avatar answered Sep 21 '22 14:09

AxelH