This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface-static methods  RSS feed

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have 2 Questions ,in interfaces.
1.Why interfacs cannot have static methods .
2.We can achieve the effect of static methods in interfaces .This code snippet i picked up from some web site but really couldnt understand it .
how & why it works.Why introduced a way to a sTATIC ,if directly not possible.?


========================================
public interface MyStupidInterface
{
public static final Runnable stupidity = new java . lang . Runnable
( ) { public void run ( ) { System . out . println ( "Hello World!" ) ;
} } ;
}
========================================
public class MyStupidApp implements MyStupidInterface
{
public static void main ( String [ ] args )
{
stupidity . run ( ) ;
}
}
========================================

thnx a lot
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding your questions
Number 1)
Originally posted by Lucky J Verma:
1.Why interfacs cannot have static methods .


As my mom says, Interfaces cannot have methods, "because I said so". But seriously, it is because the java docs say so; it is how they are defined: types without implementation that allow unrelated classes to implement them and make concrete that which is abstract.

Number 2)
Originally posted by Lucky J Verma:
2.We can achieve the effect of static methods in interfaces .This code snippet i picked up from some web site but really couldnt understand it .
how & why it works.Why introduced a way to a sTATIC ,if directly not possible.?


Interfaces don't allow methods and don't allow variables, but they do allow public constants, which in Java are declared as public static final variables. Your snippet in reality is a constant of (interface!) type Runnable, and for the duration of the program will point to one spot in memory.

It just so happens that the spot in memory that it points to happens to have an inline method, something I've never seen before. Too be honest, I'm at a loss to be fully able to explain this behavoir.

With code tags this is:

[ April 20, 2007: Message edited by: pete stein ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stupidity references an instance of an anonymous inner class. Google for it to learn more about it.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

interfaces can have objects. These are by default public and static.

MyStupidInterface contains such an object. It is the object of an anonymous inner class that implements interface Runnable.


I wouldn't call such objects constants. Objects are never constants, only primitive values can be.

By the way, MyStupidInterface does not contain own methods, so MyStupidApp doesn't have to implement them (marker interface).

Lucky wrote:
We can achieve the effect of static methods in interfaces.

No, the run() method is not static, and as it belongs to the inner class, you cannot invoke it via static (i.e. class name) access. In the App Code it is invoked via the instance stupidity.
The instance is static (so it can be called from main), but its run method is not.


Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!