aspose file tools*
The moose likes Beginning Java and the fly likes Static Methods in an Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Methods in an Interface" Watch "Static Methods in an Interface" New topic
Author

Static Methods in an Interface

jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
I fail to understand Why we cannot have 'static methods' in an Interface?
We can have a static method in an abstract class then why not an Interface?


Still Learing..
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3216
You can have implementation code in an abstract class but in an interface you only declare a method but cannot define it.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36482
    
  16
The implementation of a static method goes with the class, so you would have to implement it inside the interface, but as Arulk Pillai has told you, you mustn't implement methods in an interface.

The whole idea of an interface is to provide functionality which is implemented differently. A static method would be implemented the same in all instances.
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
Originally posted by Campbell Ritchie:
The implementation of a static method goes with the class, so you would have to implement it inside the interface, but as Arulk Pillai has told you, you mustn't implement methods in an interface.


Yes, I understand that we cannot implement a method in an interface. But we can have a class define code for a static member in an interface. e.g:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Static methods are not inherited - not from superclasses and not from interfaces.

That code doesn't work because it tries to inherit the static method from myInterface into myClass.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
Originally posted by Jesper Young:
Static methods are not inherited - not from superclasses and not from interfaces.

That code doesn't work because it tries to inherit the static method from myInterface into myClass.


I think they are inherited. Look at the code below. I can compile this and get an output:



Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

In computer science, you have to define your terms very carefully. The Java Language Specification defines "inherited" carefully this doesn't count as inheritance.

The important thing is that for static methods, the compiler figures out while the program is being compiled where the static method is actually defined, and uses that class's name in the generated code. Even though you wrote "Test.method()", the bytecode that gets generated uses "SuperClass.method()".

Anyway, the real answer as to why this isn't allowed is that it's not. At some point you just have to accept that Java is what Java is; the folks who designed the language made some choices, and you have to live with those choices.


[Jess in Action][AskingGoodQuestions]
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
Originally posted by Ernest Friedman-Hill:
Anyway, the real answer as to why this isn't allowed is that it's not. At some point you just have to accept that Java is what Java is; the folks who designed the language made some choices, and you have to live with those choices.


Thanks for your reply Ernest! I can live with that...
I was just wondering if there is some logic behind all this, something that will help me keep this in my mind instead of cramming/memorizing it.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[EFH]: In computer science, you have to define your terms very carefully. The Java Language Specification defines "inherited" carefully this doesn't count as inheritance.

I believe if you look at the JLS definition, static methods are inherited. From JLS 6.4.3: "The members of a class type (�8.2) are classes (�8.5, �9.5), interfaces (�8.5, �9.5), fields (�8.3, �9.3, �10.7), and methods (�8.4, �9.4). Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (�8.4.8)." The italics on inherited indicate this is the JLS definition of the term. There's nothing here about a method or field being static - any accessible member of a superclass or superinterface is inherited as long as it's not private, hidden, or overridden. Static methods certainly qualify.

Of course this particular definition has no effect on the rest of this discussion, which has been addressed well enough by others already.


"I'm not back." - Bill Harding, Twister
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Originally posted by jaspreet atwal:


Thanks for your reply Ernest! I can live with that...
I was just wondering if there is some logic behind all this, something that will help me keep this in my mind instead of cramming/memorizing it.


Hmmm....

Jaspreet,

I have slightly different answer to this, [may be would help you to understand it and not cram],

Interface methods are already/implicitly abstract, i.e. there is no implementation available, and abstract and static can't go to-gather.
Lets assume its possible, now consider abstract static method declared in an interface "MyInterface", now this gives a great chance to the caller to call the method by doing "MyInterface.myStaticMethod()", right ? which ofcourse would lead no-where/doesn't make sense.

To block such usage, you will have to block "static" modifier to methods in interfaces.
BTW, this is the same case with abstract methods in an abstract class too. Once again, abstract and static can't go to-gather due to the reason above.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi jaspreet,

static and abstract can not come together

example: abstract static void myMethod();//not possible

i.e, abstract (antonymous) static
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
You can't possibly override a static method, which is something interface methods are all about.



Will print "A".


Java hobbyist.
ashishs Sinha
Greenhorn

Joined: Jul 01, 2008
Posts: 3
Putting it in different words -
Even an abstract class cannot have a method which is static.
All these variations leads to the same question - "where can we define the implementation of the method".
Since static are tied with class/interface - if we cant implement the method in the class - we cant do so anywhere else - thats why compiler doesnt let us do so.
ashishs Sinha
Greenhorn

Joined: Jul 01, 2008
Posts: 3
Oops!! I meant static "abstract" method inside an abstract class
Shikhar Madhok
Ranch Hand

Joined: Dec 18, 2006
Posts: 95
Modified the code a little

This prints Hello B.

After reading the thread I expected it to print Hello A

Question for Ronald:
Doesn't this indicate that the printHello() method of A was overridden by B?


Question for Ernest:
If the compiler looks at b.printHello() as SuperClass.printHello() shouldn't it traverse all way up the hierarchy tree, until it did not find a SuperClass.printHello()?

Shikhar


He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever - Chinese proverb
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Doesn't this indicate that the printHello() method of A was overridden by B?
Not in the conventional sense no - static methods can do something called "shadowing". This means any class may possess a static method with the same name (but possibly with different return type and arguments) as any other class, and this is legal. The interpreter figures which one to invoke by the declared class type on which it is invoked. In your case, you're invoking it on a declared type of class B (b.printHello()). Hence it really invokes B.printHello() so you get the output "Hello B".

If this were true inheritence, it would print "Hello C" since the printHello() method in C would override that of B and, for instance methods it is the actual object type on which the method is invoked.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
You may also find this topic useful: what linking mechanism uses java (early or late)
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
I see what you mean, it may be confusing to think that the static method is overriden, because:



Prints "A", while if class B would have its printStuff() method uncommented, the program would print "B". But this is because the mechanism that handles static methods is different than that which handles non static methods. In the above case, the reference is used to find the class that it is based on. The compiler searches up the class chain to see if it can find a match. Objects do not come into the picture here at all, and neither does the dynamic that occurs for non static methods at runtime that allows us to use polymorphism (invoking a method that behaves differently than the method defined in the class that the reference is based on, because the actual method found is one overriden by a subclass that the actual object was instantiated from, to which the reference is referring).

As Charles mentioned very well, this is shadowing instead of overriding.
Shikhar Madhok
Ranch Hand

Joined: Dec 18, 2006
Posts: 95
Very interesting. I personally had not come across "shadowing" as a term in java until this thread.
Did I miss a few chapters, or is the term generally not used with java very often?

Anyway, will take it as a learning. Never thought abt the question though. Thanks a ton to the topic starter.

Shikhar
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36482
    
  16
Originally posted by Shikhar Madhok:
Did I miss a few chapters, or is the term generally not used with java very often?
Have a look through your book and see how much is actually printed about shadowing and overriding.
This delightful little book has some related puzzles (no 71, 73, 79, etc) in.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Did I miss a few chapters, or is the term generally not used with java very often?
It's not used that often. In practise almost all of what you do with Java will (and should) be OOP - so you'll not use static methods and variables much, less in such confusing situations.

Shadowing is the official terminology - as mentioned in the Java Language Specification: 6.3.1 Shadowing Declarations
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
In .NET shadowing (I think) is where you have a variable that has the same instance name as a default function/varaible name (maybe only one case).

does that apply for java as well?

Justin


You down with OOP? Yeah you know me!
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
It is the same definition of shadowing in .NET: that a method or variable in the subclass 'masks' one of the same name from a superclass from the compiler and is referenced in preference to the superclass one. However, .NET provides more functionality than Java in that respect - like the VB.NET "Shadows" keyword which also allows instance methods to be shadowed rather than overridden by inheritance (though I'm not sure why anyone would want to do that really).

In Java as in C++, (both instance and static) variables can only shadow each other - there is no inheritance of variables. The idea of shadowing methods in both Java and .NET is basically using non-virtual C++ methods, while overriding instance methods by inheritance is equivalent to virtual C++ methods, if that helps at all.
[ July 02, 2008: Message edited by: Charles Lyons ]
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

This was extremely helpful. I am referring it after 2 years or so . Thank you everyone


Understanding is Everything - Peter Lord
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Methods in an Interface
 
Similar Threads
interface
Static access in interface
Static methods in an interface
static Methods @ Interface
Static methods in Java Interfaces?