static methods in interfaces

  • Thread starter ballpointpenthief
  • Start date
D

Dale King

Andrew said:
Huh! I would not have believed that if my compiler
had not just confirmed it. (grumbles) ..and all
those times I type 'public abstract '...

( Still - old habits.., I'll probably *continue* to add them ;)

That applies to interface methods.

Interface fields are pulic static final whether you include those
keywords or not.
 
M

Mike Schilling

Chris said:
Andrew Thompson wrote:

[me:]
Huh! I would not have believed that if my compiler
had not just confirmed it. (grumbles) ..and all
those times I type 'public abstract '...

( Still - old habits.., I'll probably *continue* to add them ;)

FWIW, the "standard" recommendation from Sun is to omit them. I
don't normally have much time for Sun's recommendations, but in this
case I agree with them, and it seems that much of the rest of the
Java programming community do too.

FWIW, "public abstract" for interface methods is forbidden in C#, as is int
a[] (as opposed to int[] a). I suspect that, except for compatibility
issues, they'd be forbidden in Java as well.
 
P

Patricia Shanahan

ballpointpenthief said:
sorry, I meant SSCunCE.
It should now be clear why this can't be an instance method (there
might not event *be* any instances)

public interface TheInterface {
public static String getSomethingReleventToClass();

}

public Class AClass implements TheInterface {
private static String somethingReleventToClass = "This will be
different
in each class"; // *** I've changed this to static here ***
public static String getSomethingReleventToClass() {
return somethingReleventToClass;
}

}

public Class Application {
private TheInterface someClass;
public Application() {
someClass.getSomethingReleventToClass();
}
}

How is the compiler supposed to know which class to use to do the call?

If someClass is null at the time of the call, then the only type or
class information is the compile-time type TheInterface, and it does not
have an implementation of getSomethingReleventToClass. If someClass is
required to be non-null at time of the call, then the instance method
approach works.

Patricia
 
L

Lew

SSCCE refers to uncompilable examples if the purpose is to show the compiler
error, that's why the second "C" expands to "Correct (Compilable)", not
"Compilable".

I missed the first part of this thread. Did the question have to do with why
static methods are illegal in interfaces?
How is the compiler supposed to know which class to use to do the call?

If someClass is null at the time of the call, then the only type or
class information is the compile-time type TheInterface, and it does not
have an implementation of getSomethingReleventToClass. If someClass is
required to be non-null at time of the call, then the instance method
approach works.

Do I understand your response correctly that it recommends a way to do the
requested operation without using (illegal) static interface methods?
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top