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

vicente lee
vicente lee
6,271 Points

Is there a reason why the string of facts are declared inside the onClick method? Can't it be declared after the views?

I was wondering if the array of strings could be declared right after the two findViewById statements?

1 Answer

Yes, you are also right. However, during the course, the teacher mention about the "anonymous class". The reason why the teacher used the "anonymous class" is becuase, the app only needs the String variable when you tap on the button. After then, the String variable will be disappeared.

The only case you declare the String after the findViewById statement would be when other methods share the String Variable. However, in this case, you can see that only onClick uses the String variable.

If you have any further question, please leave the comment. :)