Casting object

+1 Black Boy · November 4, 2015
I've learn that casting is a way to convert variable and object into something else then I come across a line a code in a android programming book :

Button buttonPlay = (Button)findViewById(;

I tried to break it down;

Button is an object

buttonPlay is the object variable 

= assignment operator

then this:

(Button) is an argument taken from findview by id... -> here I get mess up

I mean casting object should be like this example:

Graphics comp = new Graphics(); 
Graphics2D comp2D = (Graphics2D) comp; -> by the way I know nothing about graphic object yet.

I know that ( is a reference to the designer button but simply put in this manner get confusing

So I wonder is this casting or not?

and why the button object don't have an access spacefire like private for example? 

Thanks for the help!!


Post a Reply


Oldest  Newest  Rating
0 Gary Whitney · December 18, 2015
findViewById( returns a reference to a generic Object with the id you give it.
(Button) casts it to a Button widget object which is what you nee to match the Button on the left side.
And buttonPlay is now a reference to the widget with the id.
0 Black Boy · December 19, 2015
ok thank, so there are casting object after all.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator