Welcome to the Treehouse Community
Looking to learn something new?
Ganta Hemanth6,460 Points
scope of final variable in java
For a subclass to accsses a variable outside the subclass it should be final or a member variable. So why is the subclass able to access the final variable?? How does the final keyword work??
I agree with what Wesley Seago says but think his wording may be just as confusing. So my answer is just to clarify.
private String mNotFinal = "edit this text within this class or sub class"; private final String mFinal = "this text is set and cannot be changed"; // so... mFinal = "I'm trying to change unchangeable text"; // this line will NOT work
That is how the final keyword works for the most part.
Wesley Seago10,424 Points
Using final guarantees the value won't be re-assigned. Lack of final when appropriate will usually cause a compile time error, rather than a runtime error.