Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to declare a constructor inside of anonymous class?

 
Roni Silva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to declare a constructor inside of anonymous class?



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.


 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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:
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic