Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKeve Zoltani
1,189 PointsThis is from a few videos about but question about object declarations
When we defined these objects:
final TextView factLabel = (TextView) findViewById(R.id.factTextView); Button showFactButton = (Button) findViewById(R.id.showFactButton);
I understand the encasing allows us to change the type so no error is thrown. We arent declaring new objects right because we lack the "new" keyword here? Are we defining the objects here? So showFactButton is an object of the Button class and we are somewhat pointing to it where is in the R file? And then from there we can tell what to do when we click it?
I am just having trouble understanding these two statements. Programming so far seems to be just finding the best methods to carry out certain commands or functions and I feel as if I am just following along whereas if I was asked to reproduce this it may be difficult. Any advice? Thanks
2 Answers
William Davis
1,083 PointsBreaking down the code snip you provided
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findViewById(R.id.showFactButton);
The line final TextView factLabel - that defines a new variable of Type TextView (we'll ignore the final part). we then set the variable with the second part ** (TextView) findViewById(R.id.factTextView); **
There are a couple of things going on there.
1.(TextView) this is a cast. A cast is when we are changing one type into another, compatible, type. In this case findViewById returns type View. It just so happens that a TextView extends the Type View. Because TextView inherits from View we can cast a View into a TextView.
- As I stated above findViewById() is a method call that returns type View. It accepts one parameter, an integer that is the corresponding Id for the View we want to return. Those ids are made available to us in a friendly manner using the R.id.{name} syntax
The same thing is going on with the second variable / assignment.
Button showFactButton = (Button) findViewById(R.id.showFactButton);
Button showFactButton; - creates a new variable of type Button
(Button) casts the following object to type Button
findViewById() returns an object of type View based upon the integer value you pass in (using R.id.etc)
Again here, Button is a type of View so we can cast without error.
Keve Zoltani
1,189 Pointsthank you very much! happy new year!
William Davis
1,083 PointsYou are welcome! Happy New Year!