android - Class casting and parentheses -


i have seen following code suggested number of times allowing fragment access variable in activity.

foo = ((mainactivity)this.getactivity()).getfoo(); 

now, if looked like:

foo = (mainactivity)this.getactivity().getfoo(); 

if make sense being cast (mainactivity), however, enclosing entire left side in parentheses exactly?

thanks much,

-john

just in maths, adding parentheses means "do first". in expression:

((mainactivity)this.getactivity()).getfoo(); 

we saying first:

(mainactivity)this.getactivity() 

then on result of above, this:

.getfoo(); 

if don't have parentheses,

(mainactivity)this.getactivity().getfoo(); 

we saying first, since casting has low precedence:

this.getactivity().getfoo() 

then cast result of above mainactivity.

obviously, latter expression not work because there no getfoo defined in activity, getactivity returns.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -