aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why in x=x++ x keep not changed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why in x=x++ x keep not changed? " Watch "why in x=x++ x keep not changed? " New topic
Author

why in x=x++ x keep not changed?

jack zhu
Greenhorn

Joined: Jul 10, 2001
Posts: 6
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
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);
}
}

Refer JLS chapter 12.5
http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44670
Hope this helps,
Vanitha.
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
for your first question refer this discussion http://www.javaranch.com/ubb/Forum24/HTML/009768.html


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
jack zhu
Greenhorn

Joined: Jul 10, 2001
Posts: 6
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
Thank you, Anshul Manisha! I have learn more.Thanks again!
Faisal Farooqui
Greenhorn

Joined: Jun 25, 2001
Posts: 13
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://aspose.com/file-tools
 
subject: why in x=x++ x keep not changed?
 
Similar Threads
another strange question
overriding doubt
JTip Question
what is it after all
Constructor