This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pls clarify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pls clarify" Watch "pls clarify" New topic
Author

pls clarify

bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
Can someone kindly tell me why the result is the AA's result is
20,10,30,31 and not 20,31,30,31 ?
class B{
protected int x;
B(){ x = 10;}
B(int a){ x = a;}
void m1(){x = 20;}
void m1(int x){this.x = x;}
}
public class AA {
int x;
AA(){x = 20;}
AA(int x){this.x = x;}
void m1(B x){this.x = x.x++;}
public static void main(String arf[]){
AA x = new AA(20);
B y = new B();
System.out.println(x.x);
x.m1(y);
System.out.println(x.x);
y.m1(30);
System.out.println(y.x);
((AA)x).m1(y);
System.out.println(y.x);
}
}
Thanks in advance
bani
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by bani kaali:
Can someone kindly tell me why the result is the AA's result is
20,10,30,31 and not 20,31,30,31 ?

Thanks in advance
bani


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Bani
I have stepped through the code jotting donw the values:
AA x = new AA(20); // x.x = 20
B y = new B(); // y.x = 10

x.m1(y); // x.x = 10
y.m1(30); // y.x = 30

((AA)x).m1(y); // x.x = 30 but later 31 because of x.x++ in m1
hope it helps


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pls clarify