Android Final Variables

15,692

I would say the extractedText has been set to final so it cannot be modified after it has been extracted, where as the coder is not bothered if the rectangle get's modified.

Share:
15,692
Dimitris Makris
Author by

Dimitris Makris

Updated on June 08, 2022

Comments

  • Dimitris Makris
    Dimitris Makris almost 2 years

    I was reading the source code of TextView and I met this code snippet:

    RectF mTmpRectF = new RectF();
    float[] mTmpOffset = new float[2];
    ExtractedTextRequest mExtracting;
    final ExtractedText mTmpExtracted = new ExtractedText();
    

    So, there they define mTmpExtracted as final, but not mTmpRectF.

    I have read this What does "final" do if you place it before a variable? where there is analyzed when to use final.

    Thus since both objects (mTmpRectF & mTmpExtracted) could be final in this specific case, is there any other reason (i.e. performace, etc) that only one is set to final or it is just a developer code-style?

    Thanks!

  • Dimitris Makris
    Dimitris Makris over 12 years
    So, except when it is required, assigning final to a variable is a kind of limitation/warning to the developer that he should not change this variable?
  • Blundell
    Blundell over 12 years
    Yes it's declaring that this is its final state for a reason.