Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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. :)