File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A Difficult question

 
Sricharan Modali
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic