• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to declare a constructor inside of anonymous class?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to declare a constructor inside of anonymous class?



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't.

A constructor is declared using the class's name. An anonymous class, by definition, does not have a name.

What you can do, however, is declare an instance initializer, just like you might do in a named class.


 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't declare a constructor for an anonymous class, because the name for a constructor must be the same as the name of the class. And since the class in question has no name, you can't declare a constructor for the class.

But let me answer the question which might have led to this question: how do I initialize the state of an object which is an instance of an anonymous class? And the answer is to use an instance initializer. Like this:
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you need a constructor in your anonymous class anyways? Can't you do your initialization outside your anonymous class?.
 
Roni Silva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:Why do you need a constructor in your anonymous class anyways? Can't you do your initialization outside your anonymous class?.



I dont need.
Just had a question about the language.
I've thought that it was not possible, and would just like a confirmation.

thank you
 
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
haha...@Paul and Jeff, great minds think alike, and at the same time too!
 
Roni Silva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:Why do you need a constructor in your anonymous class anyways? Can't you do your initialization outside your anonymous class?.



Explaining how the question had been originated, I had the following situation:

Class AA had two constructors, a default constructor and one with a String argument.

Creating an anonymous class, subclass of AA, i wish to call super("Hello!!") within the constructor of the anonymous class, without to pass a parameter to the subclass instantiated.

The code above prints "default". So how to print "Hello!!", Invoking the constructor with arguments ?
The code below prints "Hello!!"

Now, how to do prints "Hello!!" invoking super("Hello!!")? I would need a constructor in anonymous class.

Thus arose the question raised, and the final conclusion was that this is not possible.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer of your question is well explained here:
https://deepdivejava91.blogspot.com/2020/08/can-we-have-explicit-constructor-for.html
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic