This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static final variable initialization inside instance init block

 
jagan kay
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Paul Prusko
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • 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 .
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic