Final (Java): Difference between revisions

Content deleted Content added
Line 83:
where <code>pos</code> is an object with three properties <code>pos.x</code>, <code>pos.y</code> and <code>pos.z</code>. Then <code>pos</code> cannot be assigned to, but the three properties can, unless they are final themselves.
 
Like full [[immutability]], the use of final variables has great advantages, especially in optimization. For instance, <code>Sphere</code> will probably have a function returning its volume; knowing that its radius is constant allows us to [[Memoization|memorizememoize]] the computed volume. If we have relatively few <code>Sphere</code>s and we need their volumes very often, the performance gain might be substantial. Making the radius of a <code>Sphere</code> <code>final</code> informs developers and compilers that this sort of optimization is possible in all code that uses <code>Sphere</code>s.
 
Though it appears to violate the <code>final</code> principle, the following is a legal statement: