Why compiler shows error when I declare interface in static block in java?

100 views Asked by At

for example

public class Test
{
  static
  {
    interface ITest
    {}
  }
}

Here the interface ITest is declare within the static block... The purpose is to know why this happening Interfaces are inherently static then why it can not be declared in static block

========================================================================= Contrary, I checked for below case and it shows no error.

public class Test
{

  interface ITest
  {}
}

If someone can really point out the difference due to which there is error in one case and not in second case, then it would be great help.

1

There are 1 answers

0
A.v On BEST ANSWER

You cannot have a structure definition (class, interface, enum, annotation def, ...) in an executable block of code. The only exception is inner class.