The Artima Developer Community
Sponsored Link

Java Answers Forum
how to use the new defined methods in the Anonymous inner classes?

4 replies on 1 page. Most recent reply: Jul 11, 2005 1:33 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
zhengyi

Posts: 2
Nickname: zarger
Registered: Jul, 2005

how to use the new defined methods in the Anonymous inner classes? Posted: Jul 3, 2005 8:17 AM
Reply to this message Reply
Advertisement
for example,i use a anonymous inner class to implement a
interface.In the inner class i add some new methods,but how
can i use these methods,i don't know how to get the reference of the inner class but only the interface.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: how to use the new defined methods in the Anonymous inner classes? Posted: Jul 4, 2005 12:48 AM
Reply to this message Reply
You can't access them from the outside.

In order t use the additional methods you would have to cast the reference variable to the implementing class, because the interface does not offer the additional methods.

But because you don't give the class a name you can't cast the reference variable.

However you CAN access the methods from inside the implementing class.

zhengyi

Posts: 2
Nickname: zarger
Registered: Jul, 2005

Re: how to use the new defined methods in the Anonymous inner classes? Posted: Jul 4, 2005 6:52 AM
Reply to this message Reply
thanks,neumi

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: how to use the new defined methods in the Anonymous inner classes? Posted: Jul 7, 2005 7:29 PM
Reply to this message Reply
Anonymous class is not intended to have extra methods defined to be used from outside. However, if you just want to use it for the heck of it, then you can do that. It is possible because every anonymous class is comiled into a class file. Once you compile the code, every anonymous class also gets a name which is of the form EnclosingClass$<<a number>>. The <<a number>> is assigned in document order as 1,2,3. So, in the following Test class Test$1 is the name of anonymous class. Its access is package level, so you can use it inside the same package as Test class. There are two ways to do that.
1. Create it directly like new Test$1(t)
2. Get its reference by calling t.m3() and then cast it to Test$1.

If you just have reference to an anonymous class then you can also use reflection (not included below) to call any method on that reference.

Once you compile Test.java, the anonymous class is compiled as following.

class Test$1 {
 
	final Test this$0;
 
	Test$1(Test test) {
		this$0 = test;
		super();
	}
 
	public void m1() {
		System.out.println("Hello from anonymous class method.");
	}
}

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



public  class Test {
 
	public Object m3() {
				Object o = new Object() {
								public void m1() {
									System.out.println ( "Hello from anonymous class method.");
								}
							};
 
				return o;
	}
 
 
}
 
class AnonymousMethodTest {
	public static void main(String[] args) {
 
 
			Test$1 t1 = new Test$1 (t);
			t1.m1();
 
			// or you can write
			// Test t = new Test();
			// Test$1 t1 = (Test$1) t.m3();
			// t1.m1();
 
 
  }
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: how to use the new defined methods in the Anonymous inner classes? Posted: Jul 11, 2005 1:33 AM
Reply to this message Reply
You allways have to know in wich order the inner classes get their numbers, it's not "anonymous".

Flat View: This topic has 4 replies on 1 page
Topic: Installing Eclipse plugins (I know how, but isn't working) Previous Topic   Next Topic Topic: Casting or Conversion char to byte[]

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use