Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Bindings created for Anonymous Classes: how can I know if they represent the e same anonymous class?
Bindings created for Anonymous Classes: how can I know if they represent the e same anonymous class? [message #646183] Wed, 22 December 2010 15:12 Go to next message
Joan Robert is currently offline Joan RobertFriend
Messages: 16
Registered: December 2010
Junior Member
Hi, in the code snippet below, in the 2nd line, I'm creating a new anonymous class, that has the following binding key: 'Ltestepackage/AnonymousClasses$328;'

1.	public static void main(String[] args) {
2.	    Ball b = new Ball() {
3.	      public void hit() {
4.	        System.out.println("You hit it!");
5.	      }
6.	    };
7.	    b.hit();
8.	  }

	  interface Ball {
	    void hit();
	  }


In the 7th line, b.hit() references a method implementation of that anonymous class, however, the binding of the class that declares that method has the key: 'Ltestepackage/AnonymousClasses$Ball;' (it's different).

Is there any way to know which anonymous class I am making a reference to? That is, if two bindings represent the same anonymous class.

Since the key of the binding doesn't seem to allow me that, is there an alternative way?

[Updated on: Wed, 22 December 2010 15:13]

Report message to a moderator

Re: Bindings created for Anonymous Classes: how can I know if they represent the e same anonymous cl [message #646226 is a reply to message #646183] Wed, 22 December 2010 18:46 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 12/22/2010 8:42 PM, Joan Robert wrote:
> Hi, in the code snippet below, in the 2nd line, I'm creating a new
> anonymous class, that has the following binding key:
> 'Ltestepackage/AnonymousClasses$328;'
>
> 1. public static void main(String[] args) {
> 2. Ball b = new Ball() {
> 3. public void hit() {
> 4. System.out.println("You hit it!");
> 5. }
> 6. };
> 7. b.hit();
> 8. }
>
> interface Ball {
> void hit();
> }
>
> In the 7th line, b.hit() references a method implementation of that
> anonymous class, however, the binding of the class that declares that
> method has the key: KEY: 'Ltestepackage/AnonymousClasses$Ball;' (it's
> different).
The method is *declared* in the interface, and the binding is for the
interface, not the anonymous class.
>
> Is there any way to know which anonymous class I am making a reference
> to? That is, if two bindings represent the same anonymous class.
The variable is of type Ball, it is only at runtime that you can check
the type of the actual object.

> Since the key of the binding doesn't seem to allow me that, is there an
> alternative way?
>
>
I am not sure what you want to achieve.
Re: Bindings created for Anonymous Classes: how can I know if they represent the e same anonymous cl [message #646325 is a reply to message #646226] Thu, 23 December 2010 11:06 Go to previous message
Joan Robert is currently offline Joan RobertFriend
Messages: 16
Registered: December 2010
Junior Member
Deepak Azad wrote on Wed, 22 December 2010 13:46
The method is *declared* in the interface, and the binding is for the interface, not the anonymous class.

The variable is of type Ball, it is only at runtime that you can check the type of the actual object.


You're right.
The "AnonymousClasses" string that appears in the key, comes from the name of the class to which the code snippet belong (it's called "AnonymousClasses").

Further tests showed me that the key does indeed represent the same anonymous class.
Previous Topic:Validation of Globalization
Next Topic:Junit Test Annotation Problem
Goto Forum:
  


Current Time: Thu Apr 25 08:37:11 GMT 2024

Powered by FUDForum. Page generated in 0.03095 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top