wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the reason? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the reason?" Watch "what is the reason?" New topic
Author

what is the reason?

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


why does not compile whereas this does?



just by adding final has changed evrything why? does final make x scope longer?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Ankur kothari wrote:
why does not compile whereas this does?


It doesn't compile because the scope of an instance of the method inner class can exceed the scope of the method call itself. Meaning the local may no longer exist when it is needed.

Ankur kothari wrote:
just by adding final has changed evrything why? does final make x scope longer?


"final" does not change the scope of the local variable. However, it does make it possible for the compiler to determine whether it has been initialized, and hence, can determine if the instance of the inner class can make a copy of the value -- a value that will not changed, once initialized.

Henry


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

Joined: Sep 06, 2009
Posts: 531
can determine if the instance of the inner class can make a copy of the value -- a value that will not changed, once initialized.


could not understand this?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankur kothari wrote:
can determine if the instance of the inner class can make a copy of the value -- a value that will not changed, once initialized.


could not understand this?



If you use final keyword then you are bound to initialize that variable at declaration time, this behavior guarantees that when class Inner will use x, x will have some value, so it can copy that value to s.

If you do not use final then there is no guarantee that when inner class will use x, x will have any value.


SCJP 6
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


in this case cant the inner class copy the value of x to s? x has already been given a value.........

in K&B it was explained that an inner class cant access x because x can go out of scope when needed....but if x is made final then cant x go out of scope? how does final play here?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Final guarantees for two things:

1) x has some value.
2) x will not change in future.


so inner class can copy the value of x as x will not change in future.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
what if x is given a value but not made final.....wont this be any good to the compiler?
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
compiler simply says that i need to make x final...so be it....this goes down as a rule
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankur kothari wrote:compiler simply says that i need to make x final...so be it....this goes down as a rule


I donot know the exact reason, but I think, functions, methods, local primitive values and references are loaded in stack at runtime, while class instance is loaded in heap at runtime. And final and global variables are loaded in data section in memory.

So there is different parts of memory is communicating here, that's why I think sharing x that is a local variable with a class whose instance will be loaded in heap this type of funda has been made.

And I think data section is shared between stack and heap that's why we need to load x in data section. May be I donot know the exact reason.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
If you use final keyword then you are bound to initialize that variable at declaration time, this behavior guarantees that when class Inner will use x, x will have some value, so it can copy that value to s.

If you do not use final then there is no guarantee that when inner class will use x, x will have any value.


how is this right?

final int x;
x=10;

is legal then how is initilizing at the declaration time right?

or is the answer becuase the value of final variable cant be changed..but how does this matter?


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

how is initilizing at the declaration time right?

Yes its not right. A local variable to be used by a method local inner class has to be final and must be initialized before the declaration of the method local inner class. It doesn't need to be initialized at the time of declaration. If you make a variable final, then the compiler is sure that the value of the variable will not change after the method local inner class has been initialized. That way it can make a copy of the final local variable in the method local inner class. If the value of the local variable is allowed to be changed after the inner class is instantiated, you'll get unexpected behavior as the method local inner class will have the original value of local variable at the time of instantiation.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rizvan Asgarov
Ranch Hand

Joined: Jan 07, 2009
Posts: 451

Good explanation by the father of the inner class...


"Half of the science is to ask question"
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
thanks bro.....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Rizvan Asgarov wrote:Good explanation by the father of the inner class...

I'm not married, how can I be a father (biologically speaking I can be, but socially speaking I can't)...
 
Consider Paul's rocket mass heater.
 
subject: what is the reason?
 
Similar Threads
Inner classes -doubt on static
final local variable question
Inner class Doubts
Final variables
Inner Class Final vaiable reference