aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is an interface inside class implicitly static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is an interface inside class implicitly static?" Watch "Is an interface inside class implicitly static?" New topic
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is an interface inside class implicitly static?