18

I have such code:

public class Foo {
    public class Bar implements Parcelable {
        public static final Parcelable.Creator<Type> CREATOR =
                   new Parcelable.Creator<Type>() {
                   @Override
                   ....
        }
    }
}

Eclipse says:

The field CREATOR cannot be declared static in a non-static inner type, unless 
initialized with a constant expression

Please tell me what is it? I think it is because I have a nested class, but I don't know, how to correct the mistake.

peterh
  • 11,875
  • 18
  • 85
  • 108
Alex Zaitsev
  • 2,013
  • 4
  • 30
  • 56

2 Answers2

1

The inner class cannot have static methods... If you want to have it, you need to define Bar as static as well.

Otherwise, the field must be declared as non-static.

Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48
  • I don't think defining Bar as static will allow one to have a static variable within it. Not working for me anyway. – Marvo May 19 '15 at 21:51
1

Although I do not know why, static fields and methods in inner classes are prohibited by Java. The only way to work around this to declare a static inner class; or of course you can make your field in the nested class non-static.

Gary
  • 13,303
  • 18
  • 49
  • 71
nurgan
  • 309
  • 1
  • 4
  • 22