aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final instance variables" Watch "final instance variables" New topic
Author

final instance variables

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Instance variables get their default values at compile time. Than how can the following code be legal?

How is it possible to reassign value in final variable "c" when it is already assigned with default value 0?


Astha - OCPJP 6 (90%)
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384



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 Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

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

This is the case with local variables because they never initialized automatically. But in above code variable "c" being an instance variable gets initialized automatically with its default value. Then how can it be reassigned?
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58


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
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


java specs A blank final is a final variable whose declaration lacks an initializer.

it seems that is the case.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Yes, final fields don't get initialized to 0/null like their non-final siblings do.
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

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

Ah...i didn't know these concepts, thanks a lot.
MohamedSalim malik
Greenhorn

Joined: Feb 04, 2011
Posts: 19
There were no reassignment happened. C variable only got assigned inside the constructor. You have to initialize the final variable before constructor finished. please correct me if i am wrong. forgive my bad English .
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

Final variables are not assigned a default value when an object is instantiated.

If a value is not assigned at the same time that the variable is declared a value can be assigned in either a constructor or an initialisation block but not both. See example below:

This example shows that the final variable is assigned a value in a initialisation block.


Word of warning: if the code in the constructor or initialisation block uses the final variable then the variable must be declared in the code before it is used. See this post: http://www.coderanch.com/t/564811/java-programmer-SCJP/certification/why-code-showing-compile-time

Alex Theedom - Senior Java EE Developer (OCPJP/OCPJWCD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final instance variables