Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the reason?

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • 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?
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Rizvan Asgarov
Ranch Hand
Posts: 462
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks bro.....
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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)...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic