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 Android Lists and Adapters (2015) Custom ListViews getView() and the ViewHolder Pattern

Why was the ViewHolder class declared as a static class?

When to declare static? When non-static?

2 Answers

In general, we declare nested classes as static, when it has no dependency on the Outer class. In our case, the ViewHolder class has never referenced (accessed) any member variable from the Adapter class (Outer class), therefore we can declare it as static. To keep it simple a nested-static class is just another [Outer] class which is nested for readability, since its use is restricted to only its Outer class. Your nested class must be declared non-static (called Inner class) if it has to access the member variables of the Outer class.

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

I think ben instantiate the holder = new ViewHolder() so that it can be referenced in the setTag() method as argument cause I think its impossible to reference the ViewHolder class as the argument for the setTag()

Also why it set as static ? I think because we don't have to make the setter or getter method in that class to make changes of the member variable.