• 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

A Difficult question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
For the question What is the result of compiling and running this...

I Thought the answer wud be
A1B0A2B1A2B2
but the answer is
A1B0A0B1A0B2
Can anyone please tell me why counter is not incremented and made 2 after the calls it is incremented at
A a1 = new A();
and
void m1() {new A().new B();}
Thanks!
Sricharan
Edited by Corey McGlone: Added CODE Tags
[ April 01, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's try to go step by step.
First, we have this, which executes the constructor for A:

Then, we execute this line, which causes use to create a new A object and a new B object:

Next, we execute this line of code, which uses the original A object that we created, which has the name A0, and creates a new B object.

Finally, we execute what really boils down to the same line as the last one. We create a new B object, but we use the original A object.

That gives the total output of A1B0A0B1A0B2.
I hope that helps,
Corey
 
Sricharan Modali
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh Sorry Corey
I thought there was a Static modifier before variable name too. I just thought
cos all other variables are defined static. I overlooked it. Please remove this post from the forum as it can mislead others.
Thanks!
Sricharan
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic