aspose file tools*
The moose likes Beginning Java and the fly likes Doubt with static initializers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt with static initializers" Watch "Doubt with static initializers" New topic
Author

Doubt with static initializers

vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
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

Joined: Feb 01, 2003
Posts: 127
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

Joined: Aug 08, 2003
Posts: 135
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

Joined: Jul 27, 2004
Posts: 31
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


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.


http://home.arcor.de/hirnstrom/bewerbung
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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:

 
 
subject: Doubt with static initializers
 
Similar Threads
Chapter 7 q 13 s&k book
interfaces
What is the difference between these methods?
locking style
class 1