0

I am wondering if someone could explain something about a class cast for me.

I am playing around with Android and I have a subclass of Application named ExApp.

I want to call a method of ExApp from one of my activities, so I do:

ExApp ex = ((ExApp)getapplication());

What I don't understand is why I need a double set of parentheses? Why can't I just:

ExApp ex = (ExApp)getApplication();  

?

Thanks.

0

1 Answer 1

8

You can. The two statements are exactly the same.

Where you'd see a difference is if you were calling a method on the result, e.g.

(ExApp) getApplication().foo();

is different to:

((ExApp) getApplication()).foo();

In the first case, it's the result of foo() which is cast to ExApp; in the second, it's the result of ExApp, and the overall expression is the return type of foo().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.