This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I search about this error there are many examples and disscutions on net but I still can not understand . If I'm righ, This error means that I have to declare mentioned variable as a final variable so that in the rest of program I will not be able to change it?
If yes, what shall I do, because I have this error and I need to change the variable after the inner class has been finished.
My code is something like:
price = X;
price = Y--;
and it throws me arror in price variable at new B() inner class! I can not declare price as a final because may be later on I need to change it(in if ). what should I do?
The inner class may exist after the local variable has gone out of scope and been taken off the stack. So the inner class has to retain a copy of that local variable. What if the value changes during execution of the method? Then there might be two values to that variable. It would be very difficult for a compiler to follow the execution of that method, so it is very difficult to record the changes for the inner class. So the rules are: no changes allowed. This can only be implemented by making the local variable "final".