Astha - OCPJP 6 (90%)
naveen yadav wrote:
the thing about the final variable is ."once final variable is assigned the value afterward cannot be changed ".
It is not necessarily to declare and initialize at same time. you can initialize whenever you want but you cannot change that afterwards
Astha - OCPJP 6 (90%)
Final Fields Initialization
Final fields don't get default values, they have to be explicitly initialized.
A final variable can only be initialized once, either via an initializer or an assignment statement. If a final instance variable is not assigned a value - there will be a compiler error !
If not initialized at the point of declaration: this is called a 'blank final' variable.
A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared or an instance initializer block can be used.
Similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared.
Instance Init Blocks or constructors cannot assign values to final static variables - this will be a compiler error. Why ? An object of the class might never be created and the static final variable will be unintialized.
A final field has to be initialized in every constructor OR the compiler will complain.
Alternatively the final field can be assigned in an intializer block, however if the field is also being assigned in the constructor then the compiler complains.
Why ? Because the initializer runs before rest of the constructor body, so it will amount to reassigning the final variable which is not allowed.
Sebanti Sanyal wrote:
Final Fields Initialization
Final fields don't get default values, they have to be explicitly initialized.
A final variable can only be initialized once, either via an initializer or an assignment statement. If a final instance variable is not assigned a value - there will be a compiler error !
If not initialized at the point of declaration: this is called a 'blank final' variable.
A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared or an instance initializer block can be used.
Similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared.
Instance Init Blocks or constructors cannot assign values to final static variables - this will be a compiler error. Why ? An object of the class might never be created and the static final variable will be unintialized.
A final field has to be initialized in every constructor OR the compiler will complain.
Alternatively the final field can be assigned in an intializer block, however if the field is also being assigned in the constructor then the compiler complains.
Why ? Because the initializer runs before rest of the constructor body, so it will amount to reassigning the final variable which is not allowed.
Ref:suhrid.net/wiki-Instantiation
Astha - OCPJP 6 (90%)
Alex Theedom. Author of: Java EE Video Courses (JAX-RS, Bean Validation, Websocket, JSON-P and much more), Java EE 8: Only What's New
and Professional Java EE Design Patterns. Blogger at readlearncode.com. Twitter @alextheedom
Consider Paul's rocket mass heater. |