• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static block

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what if i declare static block as follows,

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

Thanks
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ,i got it clear
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, why cant static variables be declared inside static block??
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//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.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, a lot Harvinder.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic