aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What a Method-Local Inner Object Can and Can't Do Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

What a Method-Local Inner Object Can and Can't Do

salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
Hi to everybody..
l am reading "SCJP SCJD 1.6 Kathy Sierra & Bert Bates" book. In that book page 671 :

A method-local inner class can be instantiated only within the method where the inner
class is defined. In other words, no other code running in any other method—inside
or outside the outer class—can ever instantiate the method-local inner class. Like
regular inner class objects, the method-local inner class object shares a special
relationship with the enclosing (outer) class object, and can access its private(or
any other) members. However,the inner class object cannot use the local variables
of the method the inner class is in. Why not?

Think about it. The local variables of the method live on the stack, and exist only for
the lifetime of the method. You already know that the scope of a local variable is
limited to the method the variable is declared in. When the method ends, the stack
frame is blown away and the variable is history. But even after the method
completes, the inner class object created within it might still be alive on the heap if,
for example, a reference to it was passed into some other code and then stored in an
instance variable. Because the local variables aren't guaranteed to be alive as long
as the method-local inner class object, the inner class object can't use them.
Unless
the local variables are marked final!The following code attempts to access a local
variable from within a method-local inner class.


May anybody explain me the what expressing red coloured and big formatted sentence

thanks alot..
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
And one more thing wanna add that book saying that " But even after the method
completes, the inner class object created within it might still be alive on the heap if,
for example, a reference to it was passed into some other code and then stored in an
instance variable.
"

How is it possible outside of the metod which inner class is declared, outside of the metod "metod-local-inner-class" is unknown or invisible

Example :



please help
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

salih ayan wrote:And one more thing wanna add that book saying that " But even after the method
completes, the inner class object created within it might still be alive on the heap if,
for example, a reference to it was passed into some other code and then stored in an
instance variable.
"

How is it possible outside of the metod which inner class is declared, outside of the metod "metod-local-inner-class" is unknown or invisible


You need to distinguish between the scope of the reference, instance, and the class definition. Local reference variables and the method local class definition are no longer in scope on completion of the method. However, the instance can exist outside of the method. Modify your code to do what is suggested -- assign the instance to an instance variable prior to completion of the method.

Also, note that since the class definition is out of scope, you have to use something that is in scope -- such as an Object reference.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
Henry you say :

However, the instance can exist outside of the method. Modify your code to do what is suggested assign the instance to an instance variable prior to completion of the method.
Also, note that since the class definition is out of scope, you have to use something that is in scope -- such as an Object reference.


However, the instance can exist outside of the method. For this sentence may you give me an example code please
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

salih ayan wrote:Henry you say :

However, the instance can exist outside of the method. Modify your code to do what is suggested assign the instance to an instance variable prior to completion of the method.
Also, note that since the class definition is out of scope, you have to use something that is in scope -- such as an Object reference.


However, the instance can exist outside of the method. For this sentence may you give me an example code please



What about "Modify your code to do what is suggested -- assign the instance to an instance variable prior to completion of the method"? Can't you create the example from those instructions?

Henry
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
What about "Modify your code to do what is suggested -- assign the instance to an instance variable prior to completion of the method"? Can't you create the example from those instructions?

That is the point (red colored sentence) that l am not able to get or not able to understand...
Please give me an example....


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39


Looks like you started a duplicate topic...

http://www.coderanch.com/t/615732/java-programmer-SCJP/certification/pass-metod-local-class-instance

So, let's close this one.
Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What a Method-Local Inner Object Can and Can't Do
 
Similar Threads
Method local inner classes
Local final variables and method local inner class
Why are finals allowed for method-local classes??
Method-Local Inner Class and Local Variable
Inner