File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous constructors

 
sangeetha hariharan
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Iam a new member of this group.I came across the following question while taking a mock test.

"Anonymous inner classes have constructors"True or false?
The answer is given as false.

can anyone please explain why it is false?

Thanks,
sangeetha
 
Venkatesh D
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Hope http://www.tek-tips.com/gfaqs.cfm/lev2/4/lev3/32/pid/269/fid/1849
helps you.



Regards,
[ June 13, 2004: Message edited by: Venkatesh D ]
 
sangeetha hariharan
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ALL classes have constructors...
Even if you don't declare them yourself an implicit no-argument constructor will always be available and used.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:
ALL classes have constructors...
Even if you don't declare them yourself an implicit no-argument constructor will always be available and used.


With anonymous classes, you do have a constructor, you just can't define your own. Here is what the JLS says about anonymous constructors in §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.

- Otherwise, the first formal parameter of the constructor of C represents the value of the immediately enclosing instance of i with respect to S. The type of this parameter is the class type that immediately encloses the declaration of S. The constructor has an additional formal parameter for each actual argument to the class instance creation expression that declared the anonymous class. The nth formal parameter e corresponds to the n - 1st actual argument. 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 o.super(...), where o is the first formal parameter of the constructor, and the actual arguments are the subsequent formal parameters of the constructor, in the order they were declared.

In all cases, the throws clause of an anonymous constructor must list all the checked exceptions thrown by the explicit superclass constructor invocation statement contained within the anonymous constructor, and all checked exceptions thrown by any instance initializers or instance variable initializers of the anonymous class.

Note that it is possible for the signature of the anonymous constructor to refer to an inaccessible type (for example, if such a type occurred in the signature of the superclass constructor cs). This does not, in itself, cause any errors at either compile time or run time.
 
Shilpi M Ag
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey

So if they ask us if anonymous classes have constructors what shud we ans... I wud have said no they dont have.. becos i wud assume they are asking if in the code you can have a constructor...
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would hope that, on the SCJP exam, they would be very clear about what they are asking.

If you are asked, "Does an anonymous class have a constructor?" The answer would be "Yes, it has the default constructor."

If you are asked, "Can you define a constructor for an anonymous class?" The answer would be "No, to initialize an anonymous class, you must use a Instance Initializer."

I would expect that, on the SCJP exam, this distinction would be made very clear.
 
sangeetha hariharan
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mr.corey for a very clear explanation.........
 
Sanyev Babu
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you can't define a constructor for an anonymous class you can use an instance initializer to do constructor like operation. The only thing is that you can't pass arguments. Check this code...


You will get the output as Hello World
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic