Solved

Casting in Java, why?

Posted on 2013-05-19
3
397 Views
Last Modified: 2013-05-20
Hello Experts,

This is what I expect to be a 'noob' question, but I cannot figure out why I need to Cast this object.  I have a button in my Android app and apparently have to Cast the object...why?  Code below:
Button b = (Button) findViewById(R.id.button1);

Open in new window

FYI...I'm following a tutorial on Lynda.com and this was mentioned as important.  I would really appreciate any help to understand 'why' it needs to be Cast as a button when it IS a button already and b was type cast as a button too.

Thank you.
0
Comment
Question by:Modifier1000
3 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 39180229
though the actual view is a button, the return type of findViewById is different than Button.
0
 
LVL 7

Accepted Solution

by:
Beneford earned 500 total points
ID: 39180235
findViewById() is actually returning a View object (a Button is a subclass of TextView which is a subclass of View)
You know that the View returned is a Button, but the Java sysem can't work that out just by looking at the source code. The type check system tries to stop you doing things that aren't going to work (very useful most of the time), and the cast is how you tell the type check system that you really do know what's going on.
0
 

Author Closing Comment

by:Modifier1000
ID: 39181111
Thank you for the explanation, very helpful in giving more depth to your answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem sending email from Gmail Alias with Outlook for Android 4 69
stackato and cloud 4 74
saving voicemail from mobile phone to pc.. 4 45
table example 4 24
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
If your app took Google’s lash recently, here are the 5 most likely reasons.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now