hi there, Output: ------- if i once uncomment the statement labeled with LINE NO:1, i get the foll., compilation error.
When static variable, 's' & instance final variable, 'f' can be initialized inside 'instance init block', Why my 'static final' variable, 'sF' can't be assigned a value inside 'instance init block' ???
Can some one explain it why?
Thanks, jagan.k [ August 16, 2008: Message edited by: jagan kay ]
You get a compiler error when trying to assign a value to a static final variable in an instance initializer block and not for instance variables because static variables live in the number of only one and are so called class variables shared by all instances of the same class. Now, since the initializer block gets executed every time an object is created the variable would be initialized as many times as the constructor and init.blocks will. That is - I think - why the compiler complains about this line. Static final variables can be initialized only once.
static final variable must be initialized in the static iniltializer block (class initializer) - if not initialized in the declaration. You try to assign value to static sF variable in the instance initilizer block (non static) - and compiler complains. [ July 06, 2008: Message edited by: Ireneusz Kordal ]
hi Paul Prusko & Ireneusz Kordal, my doubt is cleared. i could realize how silly my question was. i exprimented and found the foll.,
FINAL(methodLocal-final (or) instance-Final (or) static final)vriables, once assigned a value cannot be reassigned.
-final(method local) var: (i)declared inside a method. (ii)should be initialized upon its declaration(as local var don't get default value).
-final(instance) var: (i)declared inside a class outside any method/block. (ii)gets default value,if not assigned a value upon its dclaration. (iii)even if it gets default value first, only once we can assign a value only inside an instance init block.
-static(class) final var: (i)declared insdie a class outside any method/block (ii)gets default value, if not assigned a value upon its dclaration as instance final var. (iii)even if it gets default value first, only once we can assign a value only inside a static block.
for static final variables , you must keep 2 points in mind .
1. They can be either initialized at the time of declaration
2. They can be initialized inside static block .
1. final variables , whether instance or static , do not get default values .
2. You must know that instance initializer block is executed while creation of new object. If it is static final variable then it can be invoked without creating single object , which means that without single execution of instance initializer block.
If it is the case what would be the value of static final variable which is neither initialized at the time of declaration nor in static block.
That is why Java restricts programmers that static final variables must be initialized at one of these two places .
SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop