• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to cast to Anonymous Inner Class

 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can I name anonymous class in cast? I do a trick, like comment out

and then do compilation. Now I can find my anonymous class name and put it in cast. It works flawlessly. However I have feeling I do something wrong. hey help to a java beginner.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now I can find my anonymous class name and put it in cast. It works flawlessly. However I have feeling I do something wrong. hey help to a java beginner.



Not sure what you mean here. Are you saying that you were able to cast it to the anonymous class type?

Henry
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to use the name of a class in your code, then you don't want it to be anonymous, right? Doesn't make sense to create a class with no name, and then work hard at figuring out its name. Just create a named inner class. You know you can create a class right inside a method, yes?

 
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you just need to create the anonymous class instance and call a method on it, and nothing else, then you could skip the part where you assign it to a variable. Just do this:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example, you don't even need to cast to call doSomething. That's the whole point of polymorphism.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
If you want to use the name of a class in your code, then you don't want it to be anonymous, right?


Very much so. In fact, you can't expect the name to stay the same. While today it might be mypackage.MyClass$2, it can change into mypackage.MyClass$3 quite easily if you add another anonymous class to your main class.
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for answering.
 
reply
    Bookmark Topic Watch Topic
  • New Topic