• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

does anoymous inner class have a constructor?

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

does the anoymous inner class have a constructor that accept an int attribute? What's the process of instantiate an anoymous inner class instance?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Meng Yi,
First of all constructors are not members of a class (JLS 6.4.2), so are never inherited.
And when creating anonymous inner classes you are basically defining and instatiating at the same time an instance from a class which is either an implementer or a subclass of a given interface or class, respectively.
Moreover since constructors must have the same name as its class, and
in this case the class is anonymous (has no name) there wouldn't be a way
to provide a valid a constructor for this class.
You often use anonymous inner classes usually when you only need 'on the
fly' one object from a class and you don't want to bother providing a full
definition of a class.
Hope this helps,
Max
 
Maxwell Canprich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again,
I also forgot to mention that since there is no way you can provide a valid
constructor for an anonymous inner class, you can only rely on the default
compiler provided constructor, the one that takes no args and has a call to super() (the superclass constructor with no args). So probably the aforementioned code won't pass compilation stage.
Max
 
Maxwell Canprich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOOOoooops, sorry to overlook the code....
you are doing new A(5), so it will compile...
Sorry for overlooking the code...!
Max
 
Maxwell Canprich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Here I quote something from JLS that might help:


15.9.5.1 Anonymous Constructors
An anonymous class cannot have an explicitly declared constructor. Instead, the compiler must automatically provide an anonymous constructor for the anonymous class. The form of the anonymous constructor of an anonymous class C with direct superclass S is as follows:
If S is not an inner class, or if S is a local class that occurs in a static context, then the anonymous constructor has one formal parameter for each actual argument to the class instance creation expression in which C is declared. The actual arguments to the class instance creation expression are used to determine a constructor cs of S, using the same rules as for method invocations (�15.12). The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter of cs. The body of the constructor consists of an explicit constructor invocation (�8.8.5.1) of the form super(...), where the actual arguments are the formal parameters of the constructor, in the order they were declared.


Max
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Does the anonymous inner class have a constructor that accepts an int attribute?


Yes.

The compiler automatically provides an anonymous constructor for the anonymous class.
A$1(int) { super(int); }
The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter of a constructor of the superclass.
The body of the anonymous constructor consists of super(...), where the actual arguments are the formal parameters of the anonymous constructor.


What's the process of instantiating an anonymous inner class instance?



new
Memory is allocated for the instance fields of the anonymous class and its superclasses and a reference to the new object is returned.
iconst_5
The arguments (5) of the expression new A(5){} are assigned to the anonymous constructor A1$(int) parameter variables.
invokespecial
The superclass constructor is invoked using super(int).
Further reading, JLS 15.9.5
 
Maxwell Canprich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Superb answer... Thank you Marlene!
Max
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maxwell, I didn't see your reply until after I posted mine. What I said is the same thing as the JLS section you quoted. So what I said is redundant. But I decided not to delete it, because it took so long to compose it. Marlene
 
Maxwell Canprich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene,
No, not at all. You translated
in concrete terms the prose from JLS.
I really liked the way you used the disassembler.
Really cool
Thanks!
Max
[ January 25, 2004: Message edited by: Maxwell Canprich ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Maxwell. I appreciate your comments.
(I compared the byte codes for instantiating the anonymous class with the byte codes for instantiating an ordinary subclass of A. They are the same.)
[ January 25, 2004: Message edited by: Marlene Miller ]
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic