5

I have studied some books for OCPJP 7 certification and in the inner classes chapter there were some strange/incomplete informations. I've tried to create an interface inside a method, but it seems you can't do that, you can only create classes inside a method. Is there any reason why you can't do that or it's just a missing feature?

Sample code:

public class Outer {
  public void method() {
    class C {} // allowed
    interface I {} // interface not allowed here
  }
}
Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
  • What purpose would it serve? – molbdnilo Aug 07 '14 at 10:47
  • 4
    I don't care about purpose, I care only about reasons because I want to understand the logic behind questions from OCPJP exam. – Silviu Burcea Aug 07 '14 at 10:48
  • *logic behind questions from OCPJP exam*? - Good luck with that one. Maybe it is a misconception, but from what I have seen so far, the questions *seem* to be built around scenarios that (in reality) would be solved differently - and be it just to prevent the corresponding question from arising. In real code, you never should have to wonder what the result of something like `a += --a + a++;` is. (Not meaning that **this** question is not interesting or relevant) – Marco13 Aug 07 '14 at 11:01
  • @Marco13 he he, I am just trying to cover as much as possible, I won't try to understand the language WTFs constructs, I just want to understand the logic behind logical questions :) – Silviu Burcea Aug 07 '14 at 11:04
  • @molbdnilo I don't see why we are not allowed to. I know the answer is because interfaces are implicitly static, but why must they be static? Maybe I will want to define multiple `I` implementations all inside the `method`. Now I can, but I have to define `I` outside `method`. Why if `I` is defined and used totally within `method` should I define `I` outside `method`. It seems a good practice to define things as close as possible to where they are to be used. – emory Aug 07 '14 at 11:06
  • @emory That's why I've tried to declare the interface inside the method in the first place. I am not happy with that, but you can declare your interface inside your class as private static and you're good to go. Anyway, my question was more about logic behind and keeping the compiler happy, I wouldn't declare an interface there so semantics are not important for me. – Silviu Burcea Aug 07 '14 at 11:22
  • @SilviuBurcea Declaring your interface inside your class as private static means that it is visible to the other methods. This is not the end of the world, but it is not what I want either. – emory Aug 07 '14 at 11:25
  • @emory I am out of ideas, I don't think there is a workaround for this scenario. You cannot have an interface inside a local class either, I am guessing that my solution is the best you can do. – Silviu Burcea Aug 07 '14 at 11:27
  • @SilviuBurcea it is not a big problem to me. I just don't see why it is not allowed. So you ask "Is there any reason?" I say because interfaces are implicitly static. If you then ask why interfaces are implicitly static, I say I disagree with that decision. – emory Aug 07 '14 at 11:31
  • @emory Yep, I disagree too, I guess there was a decision behind it. I will probably ask that this week too, so keep an eye on java and interface tags :D – Silviu Burcea Aug 07 '14 at 11:37
  • "What purpose would it serve?" For instance, to parametrize the interface with the method's template parameter. – Irina Rapoport Oct 25 '16 at 03:25

1 Answers1

9

If you read carefully the Java Tutorials, you will see that:

You cannot declare an interface inside a block, because interfaces are inherently static.

This means that if you have an interface, like this one:

public class MyClass {
   interface MyInterface {
       public void test();
   }
}

You will be able to do

MyClass.MyInterface something = new MyClass.MyInterface() {
    public void test () { .. }
};

because MyInterface will be explicitly static. It doesn't make sense to be tied to an instance of the enclosing class, because it just provides some abstraction which doesn't have to be bound to a specific instance or a state of the enclosing class.

Same thing goes to the case, in which the interface is nested in a method. Nothing inside the method could be (explicitly) static (because non-static methods are tied to a specific instance of the enlosing class) and thus you can't have a local interface.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147