Tuesday, July 4, 2017

Final Parameters in Java

Final Parameters

A Java method parameter can be declared final, just like a variable. The value of a final parameter cannot be changed. That is, if the parameter is a reference to an object, the reference cannot be changed, but values inside the object can still be changed. Here is an example:
public void writeText(final String text1, final String text2) {
    System.out.print(text1);    // read value of text1 parameter.
    System.out.print(text2);    // read value of text2 parameter.
}
In this method example you cannot reassign the parameters text1 and text2 to any other Strings than the ones passed as parameters when the method was called.

@reference_1_jenkov.com
Java Methods

No comments:

Post a Comment