aspose file tools*
The moose likes Beginning Java and the fly likes How to cast to Anonymous Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to cast to Anonymous Inner Class" Watch "How to cast to Anonymous Inner Class" New topic
Author

How to cast to Anonymous Inner Class

D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472


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.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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?



[Jess in Action][AskingGoodQuestions]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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:
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In your example, you don't even need to cast to call doSomething. That's the whole point of polymorphism.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Thanks all for answering.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to cast to Anonymous Inner Class