class Hello12 {
static int b = 10;
static {
b = 100;
}
}
class sample {
public static void main(String args[]) {
System.out.println(Hello12.b);
}
}
On running above code the output comes as 100 because when I called Hello class, static block is executed first setting the value of b to 100 and displaying it. But when i write this code:
class Hello12 {
static {
b = 100;
}
static int b = 10;
}
class sample {
public static void main(String args[]) {
System.out.println(Hello12.b);
}
}
Here the output comes as 10. I am expecting answer as 100 because once the static block is executed it gave b the value as 100. so when in main(), I called Hello.b it should have referred to b (=100). How is the memory allocated to b in both the codes?