• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Nested Class - Mock Question & Explanation on Dan Chisholm's website.

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Please refer question 12: http://www.danchisholm.net/july21/topic/section6/nested1.html

Question 12
class A {
private static int counter;
public static int getCounter(){return counter++;}
private static int innerCounter;
public static int getInnerCounter(){return innerCounter++;}
private String name;
A() {name = "A" + getCounter();}
class B {
private String name;
B() {
name = "B" + getInnerCounter();
System.out.print(A.this.name + name); // 1
}}
void m1() {new A().new B();} // 2
void m2() {this.new B();} // 3
void m3() {new B();} // 4
public static void main(String[] args) {
A a1 = new A();
a1.m1(); a1.m2(); a1.m3();
}}

What is the result of attempting to compile and run the program?

a. Prints: A0B0A1B1A1B2
b. Prints: A0B0A1B1A2B2
c. Prints: A1B0A0B1A0B2
d. Compile-time error at line 1
e. Compile-time error at line 2
f. Compile-time error at line 3
g. Compile-time error at line 4
h. Other compile-time error.
i. Run-time error
j. None of the above

Answer to Question 12: = C Prints: A1B0A0B1A0B2
http://www.danchisholm.net/july21/topic/section6/nested1ans.html

------------------------------------------------------------------

My question is does the line "A a1 = new A();" produces "A0"? If so, why is it not printed out? The answer starts with "A1......"

Also I do not understand the explanation provided on the answer. Why is it that line 3 - "void m2() {this.new B();}" calls "A1" and not "A0"?

Thank you.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A call to the constructor of A does not cause anything to be printed.

These are the class variables associated with A.

counter - 0 initially
innerCounter - 0 initially.

  • (1) When you execute


  • A a1 = new A();

    A call to getCounter() is made from within the constructor of A. getCounter()
    returns the current value of counter, and increments the value of counter. So now

    counter - 1
    innerCounter - 0

    The value of name associated with this instance of A is A0.
  • (2) When the line a1.m1() is executed, a new instance of A is created. So now

  • getCounter() is called again. This returns the current value of counter and
    increments counter.

    counter - 2

    The value of name associated with this new instance is A1.

    This new instance is used to invoke the constructor of B.

    getInnerCounter() is called which returns the current value of innerCounter and
    increments innerCounter.

    counter - 2
    innerCounter - 1

    The value of name associated with this instance of B is B0.

    Now a call is made to print.

    So A1B0 is printed.
  • The line a1.m2() causes a new instance of B to be created using the instance of

  • A created in A a1 = new A();

    The constructor of B makes a call to getInnerCounter() which returns the current
    value of innerCounter and increments innerCounter.

    counter - 2
    innerCounter - 2

    The value of name associated with this instance of A is A0, and the value of name associated
    with this instance of B is B1.

    So A0B1 is printed.
  • The line a1.m3() causes a new instance of B to be created using the current instance

  • of A.

    The constructor of B calls getInnerCounter() which returns the current value
    of innerCounter and increments innerCounter.

    The value of name associated with this instance of A is A0. The value of name
    associated with this instance of B is B2.

    So A0B2 is printed.

    So what gets printed is A1B0A0B1A0B2
    [ July 03, 2006: Message edited by: Keith Lynn ]
     
    Shanel Jacob
    Ranch Hand
    Posts: 112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Keith, thank you for the wonderful and clear explanation.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic