简体   繁体   中英

If Java is pass-by-value only, can I mandate a final modifier in formal parameters?

If Java is strictly pass-by-value for non primitive, isn't it better to establish coding standards like make all formal parameters of methods and constructors final? - to avoid confusion?

If Java is strictly pass-by-value for non primitive

Java is strictly pass-by-value for ALL arguments and ALL results, irrespective of type .

And if you don't understand, or don't believe me, read Java is Pass-by-Value, Dammit!

... can I mandate a final modifier in formal parameters?

Yes you could, but it would be a bad idea to do it for the reasons that you have given.

Declaring a method or constructor's formal parameters to be final means something different to the meaning that you are trying to place on it. Specifically, it means that the parameter can't be assigned to in the body of the method / constructor.

Doing this has the following consequences:

  • It will actually change the meaning of the code in a way that could cause compilation errors in some cases. (These are good compilation errors ... because they force the programmer to stop doing something that generally makes his code less clear. But fixing takes effort and requires retesting, etc.)

  • Seasoned Java programmers are not going to read into this the meaning that you intend. (Not that they should need to be reminded ...).

  • Novice Java programmers are likely to be more confused ... especially if they pick up the incorrect notion that declaring something to be final alters the argument passing semantics!

So this does not achieve your aim.

The real solution is to educate people that Java ALWAYS uses pass-by-value . (And that includes beating up people who persist in spreading false information and doubt about this ... like your question does!)


(In nearly all cases, it is good practice to not update formal parameters, and declaring them final prevents you doing this by accident. So this is good practice. But the reason it is good practice is not what you stated ... and if you did put this into a coding standard with the reasoning that you gave, you would deserve to be roundly criticised .)

And if you don't understand, or don't believe me, read Java is Pass-by-Value, Dammit!

Shahzeb commented that Java does not pass by value for non-primitives; it is pass-by-value for non-primitives in the sense that it passes the value of a reference to the non-primitive. It does not, however, copy objects when you pass them to functions.

The final modifier in formal parameter lists shouldn't affect callers one way or the other because methods receive copies of references and copies of primitives; they cannot affect reference and primitive values in caller code. The final modifier does not prevent mutable objects from being altered through the final reference. final can only be of any help to the code in the method it is in.

(Someone correct me if I'm wrong, but I believe this is right.)

One of the use of final in parameters is to avoid your parameter value to be changed by mistake when you write a method. Consider this

private method1(final String name){
    ...
    ...
    String oldname = this.name;
    name = name;
}

You forget to add this.name in name .The compiler will give error. This help you /developer to trade a logic error into a compiler error, if you make sure the input parameter cannot be modified. Thus reduce your error prone and debug time :)

Hope it helps

Just to cover other bases, because I think this is what you are thinking about:

In C++, it is possible to declare a parameter as "const", which basically means that the data contained in this object will not change as a result of calling this function (I think).

http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/PARAMS.html#constRef

Java does not have such a modifier. (In Java 7 maybe? Anyone know?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM