java - variable not available despite public method -


the float targetvolume in created in public void method, cannot resolved in public void method...

    scheduletaskexecutor.scheduleatfixedrate(new runnable() {         public void run() {             float x = (float) math.random();             if (x < 0.5){                 float targetvolume = 0;             } else {                 float targetvolume = 1;             }              scheduletaskexecutor.scheduleatfixedrate(new runnable() {                 public void run() {                     if (targetvolume.equals (1)){                         mp.setvolume(startingvolume+volumeincrement, startingvolume+volumeincrement);                     } else {                         mp.setvolume(startingvolume-volumeincrement, startingvolume-volumeincrement);                     }                 }             }, 0, 1, timeunit.seconds);         }     }, 0, 5, timeunit.seconds); 

how can fix this?

edit: can solve problem adding final modifier float:

        scheduletaskexecutor.scheduleatfixedrate(new runnable() {         public void run() {             float x = (float) math.random();             final float targetvolume=(x < 0.5)?0:1;              scheduletaskexecutor.scheduleatfixedrate(new runnable() {                 public void run() {                     if (targetvolume >startingvolume){                         startingvolume = startingvolume+volumeincrement;                         mp.setvolume((startingvolume), (startingvolume));                         }                     else if (targetvolume < startingvolume){                         startingvolume = startingvolume-volumeincrement;                         mp.setvolume((startingvolume), (startingvolume));                         }                               }             }, 0, 1, timeunit.seconds);         }     }, 0, 5, timeunit.seconds); 

however, final float targetvolume hold same value (a 0 or 1) each time task run? need value change randomly...

you have declared locally method

cheduletaskexecutor.scheduleatfixedrate(new runnable() {     public void run() {         float x = (float) math.random();         if (x <0.5){             float targetvolume =0;} 

declare member variable class have class scope. means define anywhere outside of method (usually before oncreate() activity).

read java variable scope


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -