• 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 block vs static initialization

 
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can not seem to access variable declared in a static block, but I can do when it's declared as a class static variable.

For example, the following code do not work since it can not find a,



But this solution works because a is declared as class static,


I was under the impression that static block is similar to static statement. We need to use static block if we need to execute more than one expression. But clearly I am wrong.

Some insight would be appreciate. Thanks!

 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its the scope.  A variable declared within a {} pair is only know within those enclosing {}s. Excepting public class variables
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do something like this. This is over kill for something as simple as an int, but if the static variable had needed some iteration (for example) to compute its value, then a static block would be a good place to put it.
 
Quazi Irfan
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the insight.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . a static block would be a good place to put it. . .

It might be a good idea to reserve static fields for constants, but I agree that a static initialiser is probably the best way to initialise a static field requiring anything more complicated than initialisation on the same line as its declaration. The alternativve would be to use a method to initialise it.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic