| Author |
Is an interface inside class implicitly static?
|
neeta mathur
Greenhorn
Joined: Aug 05, 2004
Posts: 22
|
|
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
Joined: Jun 16, 2004
Posts: 41
|
|
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
Joined: Jul 31, 2004
Posts: 13
|
|
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
Joined: Aug 05, 2004
Posts: 22
|
|
|
Thank you all guys
|
 |
 |
|
|
subject: Is an interface inside class implicitly static?
|
|
|