• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with static initializers

 
vinayak manda
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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.
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • 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
  • 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)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic