• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

arg constructor in anonymous inner class

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the anonymous inner classes I have ever seen have only a no-arg constructor. Is it possible to have any other kind of constructor?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Tysen wrote:All the anonymous inner classes I have ever seen have only a no-arg constructor. Is it possible to have any other kind of constructor?



No.


What would you put where the class name is supposed to go?

But then, you don't need other c'tors, since anything you might pass to them is available to the inner class anyway.



 
author
Posts: 23923
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

Jeff Verdegan wrote:

Kevin Tysen wrote:All the anonymous inner classes I have ever seen have only a no-arg constructor. Is it possible to have any other kind of constructor?



No.




Actually you can.... You just can't define new constructor signatures. You can use any constructor that is already available in the class that you are subclassing from. For example...



In this case, the compiler will create a constructor that takes a string and a boolean, and have its implementation call the super constructor that takes a string and a boolean.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Jeff Verdegan wrote:

Kevin Tysen wrote:All the anonymous inner classes I have ever seen have only a no-arg constructor. Is it possible to have any other kind of constructor?



No.




Actually you can.... You just can't define new constructor signatures. You can use any constructor that is already available in the class that you are subclassing from.




Ah, yes, of course. Can't believe I forgot that.

Sorry for any confusion I may have caused.
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. It's useful to know that. But actually, when I came up with this question, I was trying to make an anonymous inner class to implement an interface, and I couldn't think of a good name for the class, so that is why I wanted to make it anonymous. Eventually I just made a class within a method and then I gave it a real lame name, something like MyClass (don't remember). I did something like this:



This is a kind of abbreviated version of what I have. I thought about making an anonymous inner class and then have the howBig method return size, but that would mean I have to make size a final variable, which I can't because the value of size is dependent on several factors. I also thought about adding another line

That way there would be a final variable, and I could have the howBig method return asize. Would this work?
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it would. I've sometimes even used final variables for loop counters. For example:
Your example variable would be no different from this.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Tysen wrote:Thank you. It's useful to know that. But actually, when I came up with this question, I was trying to make an anonymous inner class to implement an interface, and I couldn't think of a good name for the class, so that is why I wanted to make it anonymous. Eventually I just made a class within a method and then I gave it a real lame name, something like MyClass (don't remember).


Don't forget that you can also make it private (if it's a nested class) or package-private (if it isn't).

That way people don't have to know about your awful naming standards.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Tysen wrote:I couldn't think of a good name for the class, so that is why I wanted to make it anonymous



That's not really a good reason to make it anonymous.

We make a class anonymous because it doesn't need a name, because it will never be referred to outside the context where we define it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic