0

What does it mean you have (TYPE) in the assignment if already specified the type when I init'd the var?

When is it needed if not all assignments have this?

Example:

private TextView mTvCreate;
mTvCreate = (TextView) findViewById(R.id.create);
doremi
  • 14,921
  • 30
  • 93
  • 148
  • what does your `findViewById` return ? – Jagannath Apr 01 '15 at 00:33
  • 1
    The cast is needed there because `findViewById()` returns a `View` whereas you want a subclass of `View` (`TextView`). The language forces you to explicitly write the cast because it is potentially dodgy (what if the `View` is a `ListView`?). On the other hand, if the return type of a method was `TextView` then no cast would be needed with `View v = method()` because a `TextView` can always be safely cast to a `View`. Many people think the fact that the android API forces you to explicitly cast after every `findViewById()` is a bit rubbish and could have been designed better. – Paul Boddington Apr 01 '15 at 00:40
  • Even by reading the "duplicate link", it is unclear to me when casting is needed and when it isn't. – doremi Apr 01 '15 at 00:40
  • @pbabcdefp Super helpful. This would be my accepted answer. – doremi Apr 01 '15 at 00:44

0 Answers0