• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

static block and static final / non final variables

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic