aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Inner Class" Watch "Anonymous Inner Class" New topic
Author

Anonymous Inner Class

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Hi all,

How can we invoke the Inner class methods from an Outer class and Outer class methods from an Inner class in the following code? I've commented some of the code which is not working in the following Anonymous Inner Class example but which worked fine with the Regular Inner class and Method Local Inner Class.






Thanks.
[ July 07, 2008: Message edited by: Arjun Reddy ]

Be Humble... Be Nice.
Nikos Pougounias
Ranch Hand

Joined: Jan 16, 2008
Posts: 110
The 2 commented lines work fine.

Just put them inside a method

Thanks.


Nikos' Java blog
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
But you will get a StackOverflowError. For, in short your code is So, each constructor call to OuterClass() will result in a new constructor call to OuterClass().


SCJP 5 (98%) - SCBCD 5 (98%)
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Hi Nikos,

Yeah that works.. In the below code, I will be able to access the Outer Class members from an Inner class as you said. But the main question is, How can I access the Inner class methods first from the Outer class?



Ralph.. sorry but I did not get you. Thanks guys for the replies.
[ July 07, 2008: Message edited by: Arjun Reddy ]
Nikos Pougounias
Ranch Hand

Joined: Jan 16, 2008
Posts: 110
The "Anonymous Inner Class" you have created is an instance field of Any.

To call its methods from the Any class simply write (in a method of Any)
<blockquote>code:
<pre name="code" class="core">
a.display();
</pre>
</blockquote>

To call its methods from the OuterClass just write (in a method of OuterClass)
<blockquote>code:
<pre name="code" class="core">
new Any().a.display();
</pre>
</blockquote>

To call its methods from any class in the same package with OuterClass you may write

<blockquote>code:
<pre name="code" class="core">
new OuterClass().new Any().a.display();
</pre>
</blockquote>

This happens because Any is also inside OuterClass.
[ July 17, 2008: Message edited by: Nikos Pugunias ]
Nikos Pougounias
Ranch Hand

Joined: Jan 16, 2008
Posts: 110
Here's some extra practice if you have some time...

<blockquote>code:
<pre name="code" class="core">
/**
* Outer class
*/
public class GrandParent {

public void sayHello() {
System.out.println("Hello from grandparent!!");
}

public void callParent() {
// practice
}

public void callChild() {
// practice
}

/**
* Inner class
*/
class Parent {

public void sayHello() {
System.out.println("Hello from parent!!");
}

public void callGrandParent() {
// practice
}

public void callChild() {
// practice
}

/**
* Most inner class
*/
class Child {

public void sayHello() {
System.out.println("Hello from child!!");
}

public void callParent() {
// practice
}

public void callGrandParent() {
// practice
}

}

}

}
</pre>
</blockquote>
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Holy ****,

its indeed a nice exercise...

I played for a bit and got a lot of examples, I could get it forever...

here it is:

<blockquote>code:
<pre name="code" class="core">
public class GrandParent {
public void sayHello() {
System.out.println("Hello from grandparent!!");
}

public void callParent() {

new Parent().sayHello();

}

public void callChild() {

new Parent().new Child().sayHello();
new Parent().callChild();

}

class Parent {

public void sayHello() {
System.out.println("Hello from parent!!");
}

public void callGrandParent() {

GrandParent.this.sayHello();

}

public void callChild() {

new Child().sayHello();
GrandParent.this.new Parent().new Child().sayHello();
this.new Child().sayHello();
}

class Child {

public void sayHello() {
System.out.println("Hello from child!!");
}

public void callParent() {

Parent.this.sayHello();
GrandParent.this.callParent();

}

public void callGrandParent() {

GrandParent.this.sayHello();
Parent.this.callGrandParent();

}

}

}

public static void main(String[] args) {

new GrandParent().sayHello();
new GrandParent().callParent();
new GrandParent().callChild();
/**/

new GrandParent().new Parent().callGrandParent();
new GrandParent().new Parent().sayHello();
new GrandParent().new Parent().callChild();
/**/

new GrandParent().new Parent().new Child().callGrandParent();
new GrandParent().new Parent().new Child().callParent();
new GrandParent().new Parent().new Child().sayHello();
/**/

}

}
</pre>
</blockquote>
[ July 17, 2008: Message edited by: Raphael Rabadan ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner Class