pass by reference - Why should I use the keyword "final" on a method parameter in Java? -


I do not understand the last keyword actually It is easy when this method is used on parameters

If we exclude anonymous classrooms, readability and announcement of intent, then it seems almost useless to me

Some data As the strength to remain stable, it seems as strong as it is.

  • If the parameter is primitive then it will have no effect because the parameter is passed in the form of the value in the form of the method and changing it will not have any effect outside the scope.

  • If we are passing a parameter based on context, the context itself is a local variable and if the reference has been changed from within the law, then outside it

Consider the general test examples below. This test passes, although the method has changed the value of the given reference, it has no effect.

  Understand Public Zero Test () {Collection & lt; Integer & gt; C = New Arrestist & lt; Integer & gt; (); Impact (c); AssertNotNull (c); The last collection & lt; Integer & gt; C1 = C; AssertTrue (c1.equals (c)); Change (c); AssertTrue (c1.equals (c)); } Private zero changes (collection & lt; integer & gt; c) {c = new arreelist & lt; Integer & gt; (); } Public zero-less (collection & lt;? & Gt; t) {t = null; }  

Sometimes its precision (for readability) is that variable does not change . Here is a simple example where with the last use, some possible headaches can be saved.

  public zero set test (string test) {test = test; }  

If you forget the 'this' keyword on a setter, the variable you want to set is not set. However, if you use the last keyword on the parameter, then the bug will be caught at compile time.


Comments