java - Cannot refer to a non-final variable inside an inner class defined in a different method -


Edit: I need to change the values ​​of several variables because they run multiple times from the timer. I need to keep the values ​​updated with each repetition through the timer. I can not set the prices in final form because this will prevent me from updating the prices, but I get the error mentioned in the initial question given below I have been:

I had written first what's below:

I'm getting the error "defined in a different method. And can not refer to a non-final variable. "

This is happening for the price of double calls and value value value of the object. Do you know why I get this problem? I do not understand why I need to make a final announcement. Apart from this, if you can see what I am trying to do, then what I have to do to overcome this problem.

  public static zero main (string array []) {int period = 2000; Int delay = 2000; Double lastpris = 0; Price Value Object = New Price (); Double price = 0; Timer timer = new timer (); Timer.scheduleAtFixedRate (New TimerTask) {Public Zero Run} {Price = priceObject.getNextPrice (lastPrice); println (); lastPrice = price;}}, delay, duration); }    

Java does not support the right kind, even if you like an anonymous Using the square here (New Timer Task () {...}) looks like a kind of closing.

Edit - See the comments below - Explanation is not correct in the following, KeeperOfTheSoul explains

This is the reason That does not work:.

Variables are local variables in the lastPrice and the price (main) system. After the main () method returns, the object you create with an anonymous class can be final.

When the main () method gives the local variable (such as the final value and value), the stack will be cleared, so they will not be present after the main () return.

But the anonymous class object references these variables

By the last value and value final , they are not actually actually variable, if things If you try to use the variable after cleansing, then the circumstances are very bad. But the constant compiler can then replace the use of lastPrice in an unknown class with the values ​​of the constant and value (at compile time, of course), and you will no longer have problems with reaching the equivalent variable.

Other programming languages ​​that end support, they do specially with those variables - make sure that they are not destroyed when the method is over, so even after closing They can use variables.

@good: You can:

  public static zero main (string agr []) {int period = 2000; Int delay = 2000; Timer timer = new timer (); Private Value PriceObject = New Price (); Private Double Value = 0; Public Zero Run (timer.scheduleAtFixedRate) (New TimerTask) {// main () private variable in the form of member variable instead of local variable in lastPrice = 0; ) {Value = value object.tagspropes (final value); system.out.println (); lastprest = value;}}, delay, duration); }    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -