Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Static block vs static initialization

 
Ranch Hand
Posts: 139
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!

 
Master Rancher
Posts: 4284
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: 8095
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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: 139
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: 72657
317
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic