• 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

inner classes

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}}
void m1() {new A().new B();} // 1
void m2() {new A.B();} // 2
void m3() {new B();} // 3
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?
Answer : Prints: A1B0A0B1A0B2
The counter variable in class A is static. This static counter variable becomes 1 after line 1 is executed. After line 2 is executed it becomes 0 again. how is this possible? Since it is a static variable it should have been 1. According to my understanding, the answer should have been A1B0A1B1A1B2 which i know is the wrong answer.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonali,

Method m1() creates a new instance of A class, which directly makes the A constructor code run:

The name variable of this particular instance now holds "A1" because counter was incremented by 1 when a1 object was created.
Methods

do not create a new instance of A, rather operating off of an existing object referenced by a1 variable in main().
Syntax new A.B(); is perfectly valid and is nothing more than using a fully qualified class name, syntax new B(); is also valid implying using an implicit variable this. Neither m2() nor m3() creates a new object of A class and, consequently, the A constructor doesn't run, so name remains the same. It's A0 for a1. Both methods though create new objects of B class, and that makes static innerCounter variable increment twice.
[ November 12, 2003: Message edited by: Vad Fogel ]
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The counter variable in class A is static. This static counter variable becomes 1 after line 1 is executed. After line 2 is executed it becomes 0 again. how is this possible? Since it is a static variable it should have been 1. According to my understanding, the answer should have been A1B0A1B1A1B2 which i know is the wrong answer.


Here the value of static counter gets incremented whenever a call to getCounter is made. But before it is getting changed the variable 'name' was set. (In the constructor of A , name = "A" + getCounter() . You are printing the value of 'name' after the counter is incremented. ( counter is incremented in m1 because you are creating a new object of A)
if you add one more line in the main as the first line, you might get a better idea.
try adding follwing line as the first line in the main
A a2 = new A();
This will create another object of A , so you will get A2B0A1B1A1B2
Hari
 
reply
    Bookmark Topic Watch Topic
  • New Topic