• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pls clarify

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic