Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static block and static final / non final variables  RSS feed

 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example :


The output that I get is : the static block DOES NOT get executed
and variable "finalStr" value = "UTF-8" is displayed

However the same code when executed and changing the variable :



Now the output is quite different :

1 >static block gets executed first
2 >variable "xyz" gets initialised - as a result method - "getData()" gets invoked

3 >finally variable "finalStr" value = "UTF-8" is displayed

Obviously this means that final variables behave differently / or are stored separately ?

Question being why are final variables initialised / without the static block being executed ?

My incorrect understanding was any reference to this class ( member variables / methods ) would cause the static block to be executed .

Could someone explain this behaviour ?

Regards ,
-anagha
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[anagha]: My incorrect understanding was any reference to this class ( member variables / methods ) would cause the static block to be executed .

Almost. There's an exception if the field is a constant variable. See these JLS links:

When Initialization Occurs
constant variable

Using final the way you did makes the variable into a compile-time constant expression, which means at run time there's no need to load the class that originally defined it.
[ October 14, 2006: Message edited by: Jim Yingst ]
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot reproduce this problem? Neither in Java 1.4.2 as Java 1.5.0.

In both cases (finalStr being final or not) I get the output from static initializer block, the getData method and finally the value of finalStr.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz posted October 14, 2006 04:48 AM:
I cannot reproduce this problem? Neither in Java 1.4.2 as Java 1.5.0.




Me too, neither on the original compiler nor on the compiler provided with Eclipse 3.1
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, evidently anagha didn't give us the correct code here. The problem described can be witnessed if the finalStr field is referenced from outside the class that defines it, e.g.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see Jim, I didn't know this exception.
Yes, yes, the language specification...
always worth a look!

Yours,
Bu.
 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jim ,
My apologies for the incorrect example .

As you have correctly pointed out the call was being made from outside the class and not from within .

Thanks for the pointers and once again I sincerely apologise for creating confusion with the incorrect example .

Regards ,
-anagha
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!