Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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".
 
Karthikeyan Kandasamy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response. I took care of both suggestions about the initial post.

I am able to run my program.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit: Totally misread the code.]
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!