File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static block" Watch "static block" New topic
Author

static block

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi i got it from whizlabs,

public class Static
{
static
{
int x=5;
}
static int x,y;
public static void main(String args[])
{
x--;
method();
System.out.println(x+ y + ++x);
}
public static void method()
{
y=x++ + ++x;

}
}

output is 3: here i am ok with the ++ operator,but static block has to be executed first then why x cant assign it's value as 5?
can't we declare and initialize(both) in the static block?

Please anyone expalin me what is going on here?
I know this is a simple thing but couldnt able to find out.


Thanks in advance
Preetha
Bangar Raju
Greenhorn

Joined: Jan 02, 2007
Posts: 5
Hi preetha,

you cant declare static variables inside a static block, that will result in a compiler error.Anything you want to initialize before the constructor is run should be done in static block.

In the example code you have given, x is initialized with 5 ( inside static block) but that x variable is going out of scope once the static block execution is complete.so the code in main method is accessing the x and y declared outside the static block which have default values 0.That explains why you are getting an output of 3.

Regards,

Bangar Raju
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
but static block has to be executed first then why x cant assign it's value as 5?


The concept here to understand is of variable scope.
Within the static block you are declaring and initializing a block scoped variable. Hence from the method main only the int primitive declared after the static block is visible as it's scope is wider than a block variable.
Thus the variables within static block and outside static block are two different variables and not the same.

can't we declare and initialize(both) in the static block?


Yes you can declare and initialize only non-static variables in a static block but then that would make it a block scoped variable.

If you wanted to initialize the int primitive declared outside the static block from within the static block then you could have used forward referencing and said:


[ November 23, 2008: Message edited by: Harvinder Thakur ]

thanks
Harvinder
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
what if i declare static block as follows,

static{
static int x=5;
}
i am not getting this clearly...

Thanks
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
1.static variables can NOT be declared within static or instance blocks.



2.static variables declared outside static or instance blocks can be
initialized within static and instance blocks.

3.variables declared and initialized within static or instance blocks have
block level scope i.e. they are not visible outside the block in which
they were *declared*.
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Thanks ,i got it clear
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
But, why cant static variables be declared inside static block??
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
//why cant static variables be declared inside static block?? //

Because it is already static variable.You are trying to apply same modifier for that variable. It give compiler time error.


SCBCD, SCJP & MCP
HowToAskQuestions
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
I believe the explanation is as follows. Please correct me if i am wrong:

1. Static Field As per jls a field is decalred static then it becomes a class variable. A class variable has complete visibility throughout the class.

2. On the contrary a field declared inside a static or instance block has block level visibility.

Thus creating a field inside a block with static modifier would contradict the above two statements. Hence a compile time error.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks, a lot Harvinder.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static block
 
Similar Threads
a question about static clause
Static initializer order
Doubt on static
Static????????????
SCJP Mock Question : static