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 trial

Android Build a Simple Android App (retired 2014) Getting Started with Android Accessing Views in Code

Android Javascript Confusion

What does it mean to "Initialize the getAnswerButton variable using the findViewById() method" ?

4 Answers

In programming language initialisation literally means giving a value while declaration of a type.

To understand this lets pay attention to this line

TextView label = (TextView) findViewById(R.id.label1);

There are two parts here

On the left had side (TextView label) is the declaration. A general format of the declaration is data_type variable_name.

Data_type (which represents what kind of data) can be of any data type for e.g. int, float, etc. and in our case TextView and variable_name could be any of your choice.

Now when we declare a variable, we are simply saying that label is of a type TextView. That's all. label as of now has no value stored in it.

(A lot many time when you are programming you get a compile time error that some_variable was not initialised. Which actually means that you declared the variable but never assigned any value to it)

[Excuse me for the build up but it was required for better understanding]

Now coming to the right hand side (findViewById) of the statement above. This statement puts a reference to the id value of the TextView label1 into the variable label. So after the statement executes, label will hold a reference value to the label1 TexView. You can read about differences between primitive types and reference types in Java for more clarity .

So in short "Initialize the getAnswerButton variable using the findViewById() method" ** means assigning a value (in this case a reference to id value of the TextView) through findViewById method and store it in the getAnswerLabel variable which can then be used elsewhere in the program with ease **

Hope this helps. Ask again if you have doubts.

Imgur

awesome explanation Gunjeet... thanks :)

Yes thank you for the explanation! It totally makes sense now.

Answer by Gunjeet exlplains a lot... but one more thing that is why initializiing is so much required. the reason is: if you would have watched Ben's lectures, he explains this by telling that sometimes it is better to give some value to the user using our app rather than let him see our app crash down because there some variables were left uninitialized...

So that our app doesn't behave in this way and the user doesn't frown, it is necessary to do so. :)

You are right. Lable can any valid name that you like.

habib kazemi
habib kazemi
2,041 Points

Is the 'lable' just a name and it can be anything?