• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

need explanation on the result

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

can any one explain me....



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();
System.out.println(name);
}
class B
{
private String name;
B()
{
name = "B" + getInnerCounter();
System.out.print(A.this.name + name);
}
}
void m1()
{
new A().new B();
}
void m2()
{
this.new B();
}
void m3()
{
new B();
}
public static void main(String[] args)
{
A a = new A();

a.m1();
a.m2();
a.m3();
}

}


Output is :-

A0
A1
A1B0A0B1A0B2
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all

Try this question its very interesting....

just now i find the answer???

you are all try........

if u cannot ...i am back with answer
[ November 17, 2005: Message edited by: vidya sagar ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A a = new A();// print A0,the first Object of A created


a.m1();//print A1B0;new A().new B();crate the second o of A and the first o of B
a.m2();//print A0B1:this.new B();"this" is the A of main and its conut is 0. new B(),create the second o of B,so B1

a.m3()://print A0B2 :new B(); equal to this.new B();the third B is created


maybe I am right and hope is is help
 
Mandy Hou
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry
it was this:
a.m1();//print A1 A1B0;new A().new B();crate the second o of A and the first o of B
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still can't grasp Mandy's explaination. Can anyone give more comprehensive explaination? Thanks in advance.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

I hope this explanation will help...



//After execution of A a = new A() we get the output
A0

Tracing.......

1. A0
In constructor the getCounter() method is called...
it returns 0 and got incremented by 1 i.e. counter is now 1.
value of the String name is now "A0" because the returned value 0 is appended to A.
System.out.println(name) prints A0

//after execution of a.m1() we get the output

A1
A1B0

Tracing.......
1.A1

see the method m1() here we created new object to A.
again see the constructor of A.

In constructor the getCounter() method is called..
Now it returns 1 and got incremented by 1 ie. counter is now 2.
value of the String name is now "A1" because the returned value 1 is appended to A.
System.out.println(name) prints A1

2.A1B0

By using the newly created object in method m1() we are again creating object for Inner class B
see the constructor of B.

In constructor the getInnerCounter() method is called.
Now the innerCounter returns 0 and got incremented by 1 i.e innerCounter is now 1.
Value of the String name in class B is now "B0"
Value of the String name in class A object created in method m1() is now "A1"
System.out.println(A.this.name + name) prints "A1B0"
we use A.this.name to refer the outer class variable name... by that way will get the name value of A


//after execution of a.m2() we get output
A0B1

Tracing...........

see the method m2() here we created new object to B.
again see the constructor of B.

In constructor the getInnerCounter() method is called..
Now it returns 1 and got incremented by 1 i.e innerCounter is now 2.
Value of the String name of class B is now "B1"
Value of the String name in class A object creted in main() method is now "A0"(Note:This is not the static member....)
System.out.println(A.this.name + name) prints "A0B1"

//after execution of a.m3() we get output

A0B2


Tracing...........

see the method m3() here we created new object to B.
again see the constructor of B.
In constructor the getInnerCounter() method is called..
Now it returns 2 and got incremented by 1 i.e innerCounter is now 3.
Value of the String name of class B is now "B2"
Value of the String name in class A object creted in main() method is now "A0"(Note:This is not the static member....)
System.out.println(A.this.name + name) prints "A0B2"

In this way the overall output is
A0
A1
A1B0A0B1A0B2

Regards
Hari Krishna.
 
reply
    Bookmark Topic Watch Topic
  • New Topic