aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static final variable initialization inside instance init block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static final variable initialization inside instance init block" Watch "static final variable initialization inside instance init block" New topic
Author

static final variable initialization inside instance init block

jagan kay
Greenhorn

Joined: Jun 08, 2008
Posts: 22
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 ]
Paul Prusko
Greenhorn

Joined: Feb 16, 2008
Posts: 25
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.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Jun 08, 2008
Posts: 22
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.
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
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 .


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static final variable initialization inside instance init block