Why member interface is always static?

R

Roedy Green

Interfaces have no variables. Therefore they cannot have an outer this.

They have instance implementing methods which might refer to this.

How did you interpret his question? You can't apply the word static
to an interface. Was he asking why all the members in an interface are
static finals?
 
E

EJP

Roedy said:
How did you interpret his question? You can't apply the word static
to an interface. Was he asking why all the members in an interface are
static finals?

class MyClass
{
interface MyInterface1 {}
static interface MyInterface2 {}
}

The word 'static' is redundant; both interfaces are static.
 
V

v4vijayakumar

You can't apply the word static to an interface.

agree, but wonder this works with java.

[root@cp_node1 (StkBarolo) java]#cat test.java
public class test {
static interface vijay {
}

public static void main(java.lang.String [] args) {
}
}
[root@cp_node1 (StkBarolo) java]#javap test$vijay
Compiled from test.java
public class test extends java.lang.Object {
public test();
public static void main(java.lang.String[]);
static interface test. vijay
/* ACC_SUPER bit NOT set */
{
}
}
 
C

Chris Uppal

v4vijayakumar said:
public class test {
static interface vijay {
}

public static void main(java.lang.String [] args) {
}
}
[root@cp_node1 (StkBarolo) java]#javap test$vijay
Compiled from test.java
public class test extends java.lang.Object {
public test();
public static void main(java.lang.String[]);
static interface test. vijay
/* ACC_SUPER bit NOT set */
{
}
}

When I run this javap doesn't complain about the ACC_SUPER bit, but I wouldn't
worry if it did.

The ACC_SUPER bit is a flag that indicates to the JVM that it should use the
"modern" interpretation of the bytecodes corresponding to code like
super.doSomething()
(see the JVM spec if you are interested in the details). That flag is supposed
to be set in every class generated by recent compilers (in fact, since at least
1.0.4), so I presume that's why javap complains when it finds it missing.

However, there is no reason to bother one way of the other about interfaces,
since they don't have real methods, and so can't have code with super-sends.
It seems that the 1.4.2 and 1.5 javacs (at least) don't bother to set the
ACC_SUPER flag for interfaces, whether they are nested or not. (I can't
imagine why not -- seems pointless to treat them specially)

So it really doesn't matter.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top