I can't under in x=x++ x keep not changed? and another question: class Process { byte b=127; Process() { this.methodA(); }
void methodA() { System.out.println("Value of b is = " + b ); }
public static void main(String [] args) { Processor p = new Processor(); } }
class Processor extends Process { byte b=126;
Processor() { System.out.println("Value of b = " + b); }
void methodA() { System.out.println("Value of b = " + this.b); } } What is the Output? 1.Prints Value of b = 0 and Value of b is = 126. 2.Compile-time error occurs. 3.Prints Value of b = 126 and Value of b = 126. 4.Prints Value of b = 127 and Value of b = 126. why answer is 1--Prints Value of b = 0 and Value of b is = 126.
Vanitha Sugumaran
Ranch Hand
Joined: Apr 11, 2001
Posts: 356
posted
0
Hi, I have added line numbers to explain the exeution sequence. When you create a new Processor(), the super() is invoked, from Process, when you invoke this.method it will go to Processor method, at that time the instance of the processor is not initialized, so the default value of b =0 is printed. Then the remaining part of super() is exeuted.(i.e SOP). Next, the instance variables are intialiazed in Processor.(b = 126), then the SOP is printed b = 126.
class Process { byte b=127; Process() { //(2) this.methodA(); System.out.println("Constructor in Super"); } void methodA() { System.out.println("Value of b is = " + b ); System.out.println("superMethod"); } public static void main(String [] args) { Processor p = new Processor(); } } class Processor extends Process { byte b=126; Processor() { //(1) System.out.println("Sub constructor"); System.out.println("Value of b = " + b); //(4) } void methodA() { //(3) System.out.println("Sub method"); System.out.println("Value of b = " + this.b); } }
Thank you very much,Vanitha. But I still have a question: The statement x=x++ ,why x keep no change?
jack zhu
Greenhorn
Joined: Jul 10, 2001
Posts: 6
posted
0
Thank you, Anshul Manisha! I have learn more.Thanks again!
Faisal Farooqui
Greenhorn
Joined: Jun 25, 2001
Posts: 13
posted
0
x=x++; ??? when u execute this statement u'll find no change in the value of (x), this is bcuz in this statement it will first execute (x=x) and then (x++). but there will be no change in the value of (x) bcuz final value of x is (x=x) that was assigned b4 (x++). clear??? or need more explanation....... === FAF
====<BR>FAF
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.