Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

anonymous inner class and Constructor

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the constructor for the anonymous inner class' object, which implements a interface Contents?


 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the default no-arg constructor is used.

Jim..
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:I believe the default no-arg constructor is used.

Jim..


Of which class? Interface don't have a Constructor?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The anonymous object you are instantiating with new. It can
be anonymous because in this situation you don't need a name;
you just want to capture the object so that you can use its
value() behavior that was just defined.

int val = c.value();

Since you've in effect implemented the Contents interface,
it's as if Contents isn't an interface at all, at lease momentarily,
but rather a fully implemented class. So the compiler doesn't
mind instantiating it. I wonder what c.getClass() would return?

Jim...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:The anonymous object you are instantiating with new. It can
be anonymous because in this situation you don't need a name;
you just want to capture the object so that you can use its
value() behavior that was just defined.

int val = c.value();

Jim...


Don't understand..

I want to know, the anonymous class's object is constructed by using which class Constructor?? Since it implements the interface Contents, but interface don't have any Constructor!
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, an interface does not have a constructor. But you have implemented
an interface only without giving the implementing class a name. So the
compiler allows you to refer to it as Contents, the interface's name.
After all, you did type:

new Contents() {...};

which is a constructor call. Does this help?

Jim...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, Every Anonymous class has a Constructor.



above anonymous class interpreted as



P.S. I used Decompiler
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anonymous inner classes also have constructor of their own (as seetharaman showed), its just that you are not allowed to declare the constructor yourself...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to All.......
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got those examples from Think in Java 4th edition by Bruce Eckel.

In that book, he/she explained it as "In the anonymous inner class, Contents is created by using a default constructor."

My Question: As seetharaman venkatasamy's reason, It uses its own constructor(that is the default constructor meant by Bruce Eckel)? That constructor don't have any relation to the class' constructor, in which the anonymous class is defined?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:My Question: As seetharaman venkatasamy's reason, It uses its own constructor(that is the default constructor meant by Bruce Eckel)? That constructor don't have any relation to the class' constructor, in which the anonymous class is defined?

The anonymous inner class constructor is created by the compiler. It calls the super constructor of class of which the object is created. It has no relation to the class in which it is created.
In the above code the constructor that the compiler will create in the anonymous inner class will call the constructor of Contents class no matter where this statement exists...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
The anonymous inner class constructor is created by the compiler. It calls the super constructor of class of which the object is created. It has no relation to the class in which it is created.
In the above code the constructor that the compiler will create in the anonymous inner class will call the constructor of Contents class no matter where this statement exists...


That's the problem... Here Contents is an interface, So it doesn't have any constructor?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:That's the problem... Here Contents is an interface, So it doesn't have any constructor?

You've already said that three times... The issue does not have anything to do with interfaces not having a constructor.

When you are using an anonymous inner class, you are not instantiating an interface. Interfaces cannot be instantiated. What you are doing, is creating a class on the spot, that implements the interface you specify. You are instantiating that class, not the interface itself.

The Java language has no way to specify the constructor of an anonymous inner class, there is no syntax to do that. You cannot write a constructor for an anonymous inner class. As others have explained, the compiler will generate a constructor for you behind the curtain, but you'll never see that constructor yourself.

To answer your original question:
Abimaran Kugathasan wrote:Where is the constructor for the anonymous inner class' object, which implements a interface Contents?

It's automatically generated by the compiler and there is no way to write it yourself.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper Young, for your kind explanation.... I got it....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic