aspose file tools*
The moose likes Java in General and the fly likes How to declare a constructor inside of anonymous class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to declare a constructor inside of anonymous class?" Watch "How to declare a constructor inside of anonymous class?" New topic
Author

How to declare a constructor inside of anonymous class?

Roni Silva
Greenhorn

Joined: Jan 22, 2009
Posts: 3
How to declare a constructor inside of anonymous class?




-------------------------------------------------
Rhoni B R Silva
SCJP , SCJA e SCWCD.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

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
Bartender

Joined: Jan 17, 2008
Posts: 2404
    
  28

Why do you need a constructor in your anonymous class anyways? Can't you do your initialization outside your anonymous class?.
Roni Silva
Greenhorn

Joined: Jan 22, 2009
Posts: 3
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

Joined: Jul 03, 2011
Posts: 256
haha...@Paul and Jeff, great minds think alike, and at the same time too!
Roni Silva
Greenhorn

Joined: Jan 22, 2009
Posts: 3
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to declare a constructor inside of anonymous class?