Use final liberally

Source: Internet
Author: User

Use theFinalKeyword liberally to communicate your intent.

The final keyword have more than one meaning:

    • A final class cannot be extended
    • A final method cannot be overridden
    • final fields, parameters, and local variables cannot change their value once set
In the last case, "value" for primitives was understood in the usual sense, while "value" for objects means the object ' s Identity, not its State. Once the identity of aFinalObject reference is set, it can still change its state, and not it's identity (that's, you can ' t re-point the object refer ence to some other object).

Declaring primitive field as final automatically ensures thread-safety for.

Some habitually declare parameters as final, since this was almost always the desired behaviour. Others find this verbose, and of little real benefit.

Consistently using final with the local variables (when appropriate) can is useful as well. It brings attention to the non-final local variables, which usually has more logic associated with them (for Exa Mple, result variables, accumulators, loop variables). Many find this verbose. A reasonable approach is to occasionally with final for local variables, but only if there is some unusual conditi On, whereby making final explicit can call attention to at least one non-final local variable in the Met Hod This serves to quickly distinguish the non-final local variables from the others.

Using Final:

    • Clearly communicates your intent
    • Allows the compiler and virtual machine to perform minor optimizations
    • Clearly flags items which is simpler in behaviour – final says, "If you're looking for complexity, you won ' T find it here."
Example
Import Java.util.*;import java.lang.reflect.field;/** This class cannot is extended, since it ' s final. */public Final class Boat {public Boat (final String aName, final int alength, final Date adatemanufactured) {fName =    AName;    Flength = Alength;    Make a defensive copy of the date fdatemanufactured = new Date (Adatemanufactured.gettime ());    does not compile, since the items is final://adatemanufactured = null;  alength = 0; }/** cannot be overridden, since the class itself is final. */public void SetDate (final Date anewdate) {//even Though the field is final, it state can change:fdatemanufactu    Red.settime (Anewdate.gettime ());  does not compile, since field is final://fdatemanufactured = anewdate; }/** Return the highest race score. */Public Integer Bestracescore () {//the result reference can ' t is final, since it can//re-pointed to Differen     T objects Integer result = integer.valueof (0); Final integer result = integer.ValueOf (0); doesn ' t compile//this example is artificial, since Fracescores could being//referenced directly here ... fin    Al list<integer> scores = fracescores;    for (Integer score:scores) {if (Score > result) {result = score;//re-point to the Max value}}  return result; }    //..  elided//Private private final String fName;  private final int flength;  Private list<integer> fracescores = new arraylist<> ();   Private final Date fdatemanufactured;}

  

Use final liberally

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.