• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

final instance variables

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 384
MyEclipse IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

it seems that is the case.
 
Saloon Keeper
Posts: 13810
311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, final fields don't get initialized to 0/null like their non-final siblings do.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Author
Posts: 116
11
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: https://coderanch.com/t/564811/java-programmer-SCJP/certification/why-code-showing-compile-time
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic