android - Java Scope Questions from a C++ background -
i'm working on first android app coming 2 years of c++ school.
the code looks this:
double total = 100000000000l; for(long = 0l; < diff; i++) { total += 1.8; } countview.settext(numberformat.getinstance().format(total)); final handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { if (total += 1.8 < 200000000000l) { handler.postdelayed(this, 1000l); return; } handler.removecallbacks(this); } }, 1000l); in c++, i'd able reuse total variable no problem - it's in same scope. in java i'm getting error message i'm attempting access inner class. trying declare total public or static gives error modifier isn't allowed here.
why can use total right below declare not several lines down?
in java, if want access members of enclosing class (i.e. total in example) local class runnable in example, these members have declared final.
https://docs.oracle.com/javase/tutorial/java/javaoo/localclasses.html
for code, instead of having double total, rewrite code (if can) total becomes integer , use final atomicinteger total. if cannot rewrite, atomicdouble alternatives.
Comments
Post a Comment