Sunday, 11 August 2013

Difference between static variable declared inside a static block and outside a static block in java

Difference between static variable declared inside a static block and
outside a static block in java

What is the difference between a static variable declared outside a static
block and a variable declared inside a static block? (considering the code
segment)
A code segment is as follows:
class A{
static int i = 10; //line 1
static { int i = 20;} //line 2
public static void main(String[] args) {
System.out.println(A.i); //output is 10
}
}
2.How to access variable 'i' in line 2?

No comments:

Post a Comment