Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView: Inner classes cannot have static declaration

I am a little confused. I've set up a RecyclerView as per the tutorial on google/android site and I get the following error:

 Inner classes cannot have static declaration

Of course, I do have a nested static class but this is how android/google defined it.

  public class ItemsViewAdapter extends RecyclerView.Adapter<ItemsViewAdapter.ViewHolder> {
       // ... 
       // ...
       public static class ViewHolder extends RecyclerView.ViewHolder {
           // ...
       }

Why am I getting this error? I hear its better to use static for nested classes so you are not wasting a reference, but the current version of android studio is complaining

Any ideas?

like image 653
Martin Avatar asked Aug 12 '15 04:08

Martin


People also ask

Why inner classes Cannot have static declarations?

Inner Classes As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Do inner classes have to be static?

Inner classes are non-static member classes, local classes, or anonymous classes. In this tutorial you'll learn how to work with static member classes and the three types of inner classes in your Java code.

Can static inner class have non-static methods?

A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested.

What is a static inner class?

A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.


1 Answers

Straight to your questions:

  1. Inner classes cannot have static declaration

    That's completely true. This is not a bug, and the error message is not even misleading.

  2. I hear its better to use nested class as a static so you are not wasting a reference

    You are absolutely correct.

  3. 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/

like image 108
MKJParekh Avatar answered Oct 10 '22 12:10

MKJParekh