File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  how to create anonymous inner class for a class without no-arg constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " how to create anonymous inner class for a class without no-arg constructor" Watch " how to create anonymous inner class for a class without no-arg constructor" New topic
Author

how to create anonymous inner class for a class without no-arg constructor

Karthikeyan Kandasamy
Greenhorn

Joined: Apr 17, 2011
Posts: 14
If i try to execute the below code, am getting compiler error. I think, its because super class doesnt have no-arg constructor. Is there any way to create anonymous inner class for a class without no-arg constructor?

Code:
-------


Error:
AnonInner.java:23: cannot find symbol
symbol : constructor A()
location: class paraminner.A
A a = new A() {};
^
AnonInner.java:23: cannot find symbol
symbol : constructor A()
location: class paraminner.A
A a = new A() {};
^

Source: - None. While reading about inner classes, i had this question.

Thanks
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Please use Code Tags.

You have overloaded the constructor with A(int i) constructor and hence default no argument constructor is not present. But you try to instantiate by calling the no-arg constructor which doesn't exist.

You could add a no arg constructor explicitly.

Ashwin Sridhar
SCJP | SCWCD | OCA
Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42

As henry is repeatedly mentioning in every post , please quote your sources webpage. And also use code tags.

Since you have only constructor with int parameter, you can have the ananomous inner class by passing any int value to the constructor of class A. Example code below.



Save the file as AnonInner.java and when ran it will print "defined in anon inner".


Thanks,
Suri
Karthikeyan Kandasamy
Greenhorn

Joined: Apr 17, 2011
Posts: 14
Thanks for your response. I took care of both suggestions about the initial post.

I am able to run my program.
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
[Edit: Totally misread the code.]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create anonymous inner class for a class without no-arg constructor
 
Similar Threads
can not find methods
doubt regarding constructors
Doubt with overloaded super constructor
Overloaded Constructor Not Working - Cannot Find Symbol
Add ActionListener to open new JFrame + error