• 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:

Doubt with static initializers

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
static {int a=5;}
public static void main(String args[]{
System.out.println(a);
}
}

Is this legal,is it true that a variable declared in a static initializer is not accessible outside the enclosing block.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vinayak manda:
public class Test {
static {int a=5;}
public static void main(String args[]{
System.out.println(a);
}
}

Is this legal,is it true that a variable declared in a static initializer is not accessible outside the enclosing block.



Extend this concept beyond a static variable to any variable that is declared within the curly braces. The curly braces provide the scoping context for the variable lifetime. It really doesn't matter if it is a static variable or not. For example, if I add a bit more code to your example:

I will get the same error as I did for a.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It really doesn't matter if it is a static variable or not.



But what does static {int i=0} mean?
 
vinayak manda
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is way of initialising the static variables in a method block.And where i as defined in the block is one copy per class (NOT ONE PER OBJECT)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example, a is *not* a static field, but a *local* variable of the static initializer.

A static initializer is simply a block of code that will be executed when the class is loaded.
 
vinayak manda
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean that I can declare both local variables and static variables in static initializers.
I shouldn't be declaring instance variables in it ,right?.
As class is loaded,static block will execute ,it will initialize the variables to thier defautls when not initialized expicitly.

Correct me If I'am wrong.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any variables declared in a static initializer will be local to that block. You can initialize -- assign a value to -- a static variable in such a block, but it must be declared at class scope.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Perhaps this is more clear to you.
b and c are declared outside the static initializer.
You need a static block only to use conditionals (if, while, for, ...).
static b = Foo.getSize (); might be declared and initialized without a static block.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stefan Wagner:
You need a static block only to use conditionals (if, while, for, ...).



They are also often used to initialize collections:

reply
    Bookmark Topic Watch Topic
  • New Topic