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

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -