RecyclerView: Inner classes cannot have static declaration
Straight to your questions:
-
Inner classes cannot have static declaration
That's completely true. This is not a bug, and the error message is not even misleading.
-
I hear its better to use nested class as a static so you are not wasting a reference
You are absolutely correct.
Solution for you:
Create a new class(File) in your project for
ItemsViewAdapter
and there won't be such an error.
General Discussion
Java and Android both supports that you can declare static
inner classes/members/functions, BUT that class should be a parent class. You cannot do that inside an inner class.
I.e., class Main
can have static class Adapter
but if the Adapter
class is an inner class of Main
is not static then it can't have its own static inner class/member.
What You Can Have?
class Main
static class Adapter
static class Holder
Or
class Adapter
static class Holder
If you want to declare any member of class as static
then the immediate parent class must be the top-level, main class in that file.
Why?
Quoting another answer, It's because an inner class is implicitly associated with an instance of its outer class, it cannot define any static methods itself. Since a static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference, it's safe to declare static methods in a static nested class.
Further Reading on the Topic
1 http://www.geeksforgeeks.org/inner-class-java/
2 http://www.javaworld.com/article/2077372/learn-java/static-class-declarations.html
3 http://viralpatel.net/blogs/inner-classes-in-java/