aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous constructors" Watch "Anonymous constructors" New topic
Author

Anonymous constructors

sangeetha hariharan
Greenhorn

Joined: May 28, 2004
Posts: 11
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

Joined: Jun 13, 2004
Posts: 2
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

Joined: May 28, 2004
Posts: 11
Thank You
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
ALL classes have constructors...
Even if you don't declare them yourself an implicit no-argument constructor will always be available and used.


42
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Shilpi M Ag
Ranch Hand

Joined: May 07, 2004
Posts: 40
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: May 28, 2004
Posts: 11
Thank you Mr.corey for a very clear explanation.........
Sanyev Babu
Ranch Hand

Joined: Dec 18, 2003
Posts: 132
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous constructors