This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
1: public class Q8 2: { 3: int i = 20; 4: static 5: { 6: int i = 10; 7: 8: } 9: public static void main(String[] args) 10: { 11: Q8 a = new Q8(); 12: System.out.println(a.i); 13: } 14: }
in my opinion it should be 10 as static block is loaded first bt it was 20 ....Please explain
public class Q8 { int i = 20; static { int i = 10; } public static void main(String[] args) { Q8 a = new Q8(); System.out.println(a.i); } }
Here this code compiles fine
1.int i 20 (instance block) 2.when the static block is run here the int i=10; is intiliazed but lost after the exceution of the STATIC BLOCK. 3.o/p is 20 ABout static blocks K&B pg.224-226
public class Q8 { { int i = 20; } static { int i = 10;
}
public static void main(String[] args) { Q8 a = new Q8(); System.out.println(a.i); } }
But compile this it wont(for your understanding) because the i is within instance block which will not be accessible as Instance block and static block are excution oriented.
saravana.T kumar
Greenhorn
Joined: Jul 13, 2007
Posts: 23
posted
0
Originally posted by saravana.T kumar: public class Q8 { int i = 20; static { int i = 10; } public static void main(String[] args) { Q8 a = new Q8(); System.out.println(a.i); } }
Here this code compiles fine
1.int i 20 (instance ) 2.when the static block is run here the int i=10; is intiliazed but lost after the exceution of the STATIC BLOCK. 3.o/p is 20 ABout static blocks K&B pg.224-226
public class Q8 { { int i = 20; } static { int i = 10;
}
public static void main(String[] args) { Q8 a = new Q8(); System.out.println(a.i); } }
But compile this it wont(for your understanding) because the i is within instance block which will not be accessible as Instance block and static block are excution oriented.
Static blocks cannot access instance variables. Stop and think about it for a second. If their are 7 instances of the class, which instance would it access, umm, arrr, yeah, as such remember it like this
static{} can only call static methods and access static variables
{} //Initialisation block can access static methods, static variables, instance variable (this) and instance methods (this)
1: public class Q8 2: { 3: int i = 20; 4: static 5: { 6: int i = 10; 7: 8: } 9: public static void main(String[] args) 10: { 11: Q8 a = new Q8(); //Line 1 12: System.out.println(a.i); 13: } 14: } [/QB]
you are right that static block will be executed first, but called before creating object.
here there is a need to remember a single point 1.static variable cannot be referenced with object. so a.i cannot be a static variable
Thanks [ July 15, 2007: Message edited by: madhu v pe ]
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Originally posted by madhu v pe: 1.static variable cannot be referenced with object. so a.i cannot be a static variable
That is not correct.In this case, the important point to remember is, that the compiler only uses the type of the reference variable, and not the value.
When a variable is declared in the initializer block it is only within the scope of that block.You check with giving any type of variables.Scope is the thing which matters.Put a out.print in that block..Got it!!
Originally posted by Ganesh Kumar: When a variable is declared in the initializer block it is only within the scope of that block.You check with giving any type of variables.Scope is the thing which matters.Put a out.print in that block..Got it!!
True and that is the reason following code will not compile.