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 anonymous inner class and Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "anonymous inner class and Constructor" Watch "anonymous inner class and Constructor" New topic
Author

anonymous inner class and Constructor

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Where is the constructor for the anonymous inner class' object, which implements a interface Contents?




|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
I believe the default no-arg constructor is used.

Jim..


BEE MBA PMP SCJP-6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jan 09, 2008
Posts: 525
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jan 09, 2008
Posts: 525
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

Joined: Jan 28, 2008
Posts: 5575

Technically, Every Anonymous class has a Constructor.



above anonymous class interpreted as



P.S. I used Decompiler
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Anonymous inner classes also have constructor of their own (as seetharaman showed), its just that you are not allowed to declare the constructor yourself...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks to All.......
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 16, 2005
Posts: 14432
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Jesper Young, for your kind explanation.... I got it....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: anonymous inner class and Constructor