• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anonymous class or Inner class  RSS feed

 
Nitin Gupte
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are conditions like in Swing applications where you can either make an Inner class or you can use an Anonymous class. Which is a better programming practice to make an Inner class or an Anonymous class?
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitin Gupte:
There are conditions like in Swing applications where you can either make an Inner class or you can use an Anonymous class. Which is a better programming practice to make an Inner class or an Anonymous class?


Dear Nitin,

What is your contribution for this topic, I mean to say that what you have found so far as your expereince.

Have you tried any one of them or you haven't tried anyone till so far.
If no then you check following links.

Which is better Anonymous class or Inner Class

I hope you find it helpful.
 
Nitin Gupte
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried both.. and when i used NetBeans IDE and selected Refactor, one of the available options was to convert Anonymous class to Inner class.. wht i want to know is that
Is Use of Inner classes more favourable than Anonymous classes?
If it's not so then why refactor?
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitin Gupte:
I tried both.. and when i used NetBeans IDE and selected Refactor, one of the available options was to convert Anonymous class to Inner class.. wht i want to know is that If it's not so then why refactor?


That may be an IDE specific topic.......


Anyways one use of refractor is to refactor anonymous class to Inner Class.

And the reason behind may be just simple form.

Actually as per my expereince designing inner class is more readable and understandable for me rather then anonymous class .

But as I said this may be only my personnal opinion. So you may find it according to yourself.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post the same question to more than one forum. See here for an explanation of why I just deleted the other copy of this thread.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you wanted to hook the same code to multiple events you would be better off with a named inner class than an anonymous inner class.

I also have a rule just based on how the code looks - if an event handler is just a few lines long (say 5 or less) then I'll just embed it inline with an anonymous inner class. If it gets any longer, I think it makes the code harder to read and I'll break it out into a named inner class.

My 2 cents (actual retail value may vary)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Nitin]: Which is a better programming practice to make an Inner class or an Anonymous class?

Which is better, an apple or a fruit?

All anonymous classes are also inner classes, so this question is kind of meaningless. Tim's answer above correctly distinguishes between anonymous classes and named inner classes, which is probably what you meant. I also agree with Tim's reasoning - except for longer classes I will often use a static nested class (which is named, and not an inner class). Actually it's rare that I use named inner (nonstatic) classes - I only do that if I need access in outer instance data. and there's a constant danger that the outer plus inner classes together are a bit too complex to easily understand. With a static nested class, there are fewer interconnections possible, and thus it's often easier to understand a static nested class on its own.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!