• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static final variable initialization inside instance init block

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Regards,
Paul.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
jagan kay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


Thanks to all.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jagan ,

for static final variables , you must keep 2 points in mind .

1. They can be either initialized at the time of declaration

OR

2. They can be initialized inside static block .

Reasons...........

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 .
 
reply
    Bookmark Topic Watch Topic
  • New Topic