0

I have an inner class which looks like this:

class Inner {
    final static String ok = "something";
    final static String nok = new String("something");
}

A compile time error occurs in the second declaration but not in the first one. Seems as if non-static inner classes not allow explicit invocation of constructor. Also static initializer is not allowed.

Can anyone explain the background?

funie200
  • 3,294
  • 5
  • 19
  • 31
user2609605
  • 315
  • 1
  • 12

1 Answers1

0

If you make your inner class static it would work:

public class Outer {

   static class Inner {
       final static String ok = "something";
       //Now this is ok also
       final static String nok = new String("something");
       }
}

You must explicitly declare inner class to be static if you want to allow explicit invocation of a constructor.This is because inner class is not explicitly or implicitly declared static and your job is to mark it as such if you want to use constructor invocation inside it.