• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

what is the reason?

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why does not compile whereas this does?



just by adding final has changed evrything why? does final make x scope longer?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what if x is given a value but not made final.....wont this be any good to the compiler?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compiler simply says that i need to make x final...so be it....this goes down as a rule
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 462
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation by the father of the inner class...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks bro.....
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)...
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic