-3

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.

027
  • 1,553
  • 13
  • 23
  • (What would be the point/purpose of this?) – user2864740 Jun 21 '15 at 04:54
  • It allows to declare interface in top level class for example public class Test { interface ITest{} } but it do not allow declaration in block public class Test { { interface ITest{} } } what is the difference in these two cases? – 027 Jun 21 '15 at 05:02
  • 1
    OK, well that's a different question. In the first case, you're just declaring an inner interface. In the second case, you're putting an interface declaration in a place where only runnable code can live. I don't know what it would mean to "run" an interface declaration; and neither does the compiler. – Dawood ibn Kareem Jun 21 '15 at 05:06

1 Answers1

3

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

user2864740
  • 60,010
  • 15
  • 145
  • 220
A.v
  • 734
  • 5
  • 26