Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is an interface inside class implicitly static?

 
neeta mathur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
While I was going through Dan Exams i got confused with the interfaces concept. In the following prg when i compile i get the error as
"Inner classes cannot have static declarations"
As per my understanding member type declarations inside an Enclosing Interface are implicitly static But not the one's inside enclosing class
Then why am i getting this error. Or is it that i'm missing something here
Pl help .
Following is the program
public class test {
class Inner {
interface myinterface { void m6(); }
public void m1() { System.out.println("inside inner"); }
}//inner
public void m() { System.out.println("in outer");
Inner i = new Inner();
i.m1();
}//m()
public static void main(String args[]){
test o = new test();
o.m();
}
}
 
n.chenththuran
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi neeta,
u cant declare an interface inside the inner class. it should be under a top level class. well, just try this out..hope it works


package com.test;

/**
* @author chenththuran
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Test {

interface myinterface { void m6(); }
class Inner {

public void m1() { System.out.println("inside inner"); }
}//inner
public void m() { System.out.println("in outer");
Inner i = new Inner();
i.m1();
}//m()
public static void main(String args[]){
Test o = new Test();
o.m();
}
}
 
francis joseph
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

you are right, interfaces are implicitly static. An inner class can contain static members only if it's a static inner class. Hence if you modify your inner class declaration to include static modifier, your program will run.

Joe
 
neeta mathur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic