wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's anonymous class Q 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 "Dan Watch "Dan New topic

Dan's anonymous class Q

aishwarya mathur

Joined: Sep 03, 2004
Posts: 22
Hi all,

This below code is from Dan's mock,anonymous class section.

Compile-time errors are generated at which lines?

The answer is 3 and 5.The explaination given by dan for this is :

Local method variables and method parameters are stored on the stack and go out of scope after the method is exited. Although a local reference variable is stored on the stack, the referenced object is stored on the heap; so the object can continue to exist long after the method runs to completion. An object that is instantiated within a method or block is not permitted to refer to a variable that is declared within the method or block unless the variable is declared final and "the variable declaration precedes the creation of the object".

Please find the double quoted sentence in the above para,Can anyone modify the code and show how the statement on line 6 can be coded so that it precedes the creation of object.
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175

however dont confuse this behavir with standard initializers though.

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51
Just to confirm, there was nothing wrong with line //6 from your code example. Variable l2 is final and it DOES precede creation of the object that is an anonymous inner class. The change to the code in the 1st response caused line 6 to fail to compile because the declaration of l2 DID NOT precede creation of the object (anonymous inner class).

Mark Patrick<br />SCJP 1.4
I agree. Here's the link: http://aspose.com/file-tools
subject: Dan's anonymous class Q
It's not a secret anymore!