Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Final Variables

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!

like image 288
Dimitris Makris Avatar asked Dec 16 '11 21:12

Dimitris Makris


People also ask

What is a final variable?

You can declare a variable in any scope to be final. . The value of a final variable cannot change after it has been initialized. Such variables are similar to constants in other programming languages.

What happens if the variable is final?

Final variables If a variable is declared with the final keyword, its value cannot be changed once initialized.

What is final in Android Java?

August 2022) In the Java programming language, the final keyword is used in several contexts to define an entity that can only be assigned once. Once a final variable has been assigned, it always contains the same value.

Can we assign final variable?

In Java, non-static final variables can be assigned a value either in constructor or with the declaration. But, static final variables cannot be assigned value in constructor; they must be assigned a value with their declaration.


1 Answers

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.

like image 109
Blundell Avatar answered Oct 20 '22 21:10

Blundell