Android

5 thoughts
last posted Feb. 27, 2016, 12:54 p.m.

2 earlier thoughts

0

Variable Scope

(for python developers)

A bit of a Java refresher, this is a thing that's actually fairly different from Python.

Even where logically in Python a variable would always be defined when it is accessed further down in the flow of your program, the Java compiler will not necessarily agree with you:

(this isn't just a code inspection thing, actually trying to compile this will report the same error).

This might be annoying in some cases but it makes sense the way that scopes work in Java:

foo is declared and only lives inside the try/except/finally blocks. It's a local variable and therefor inaccessible anywhere else.

The solution would be to declare the variable outside of these blocks:

The function will the normally return "boom" as the it's set in the finally statement :)

2 later thoughts