aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "reference" Watch "reference" New topic
Author

reference

arivu mathi
Ranch Hand

Joined: Jun 26, 2006
Posts: 38
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
}}
public static void main(String[] args) {
new A().new B(); // 2
A a1 = new A();
a1.new B(); // 3
a1.new B(); // 4
}}


can anyone explain the output plz???
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

(Edited no need to quote post just above)

What sort of confusion do you have??
[ July 19, 2006: Message edited by: Barry Gaunt ]

The Best way to predict your future is to create it - Every great individual common man
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

System.out.print(A.this.name + name); // 1
}}
public static void main(String[] args) {
new A().new B(); // 2
A a1 = new A();
a1.new B(); // 3
a1.new B(); // 4


Explanation for Line 1 & 2 #
"A.this.name" will refer to name variable of A class (Outer Class) and "name" is referring to inner class's variable.
that's why the output is "A0B0" In both classes now counter is reached to 1.

Explanation for Line 3 #

Now this line


Now this will print "A1B1" both counter increased to 2.

Explanation for Line 4 #

Now this time we are not creating any new object of outer class. so "Sample.this.name" will be ramain same as for line 3 # but still we are creating a new Object of Inner Class. that's why this time inner class counter will be increased after giving the output:

"A1B2"



This is the whole explanation of the code..

If still any concern then revrert back..with specific query.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reference
 
Similar Threads
need explanation on the result
Static Inner Class Challenge
Inner Class Doubts
Inner class.....
inner class