Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces and Static and design advice request

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

Why can one not do the following?

public interface AllFruit{
public static void makeFruit();
}


This is not a homework question - here is the whole story...
For the app, there is Module1 and Module2. Now M1 exists on it's own but M2 must have M1. That is, M2 will never exist on it's own. In M2 there are a list of functions that the user can choose. When the user selects a function, the corresponding screen in M1 is opened.

For this, for every screen, M2 needs to know how to open it. So I wanted to put this in the screens of M1

public static void openScreen(){
//code to open this screen
}

And to store these screens in a list I wanted them to implement an interface that had this and other methods, but I see that I can't have static methods in an interface.

Any suggestions?

Many kind regards,
Rachel
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution is to put the static methods in a class. Is there a reason for that not to work? Keep in mind that static methods cannot be overridden -- only hidden. The compiler uses the type definition of a variable to choose which method to call.Because bar's type is defined as Foo and print() is a static method, bar's runtime type (Bar) is not taken into account. Can you determine what will happen at the ??? line?
[ October 19, 2004: Message edited by: David Harkness ]
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can one not do the following?

public interface AllFruit{
public static void makeFruit();
}



Because method of interface MUST have public and abstract only.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i put the static methods in a class, it would follow that I would have to have the screens extend that class, but the screens already extend either JDialog, JFrame or whatever else. That was why I wanted to use an interface.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rachel Swailes:
If i put the static methods in a class, it would follow that I would have to have the screens extend that class, but the screens already extend either JDialog, JFrame or whatever else. That was why I wanted to use an interface.

Hi Rachel,

You call static methods in other classes by prefacing the method with the name of the class and a dot. Similar to how you call the static printstream class in System with System.out etc.

Hope that answers what you were asking.

Ed
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do understand how to call the static methods, but thanks anyway.

In the end I have gone with taking the openScreen method out of the individual screens and building it into the actual function, so that the function that the user selects will know how to open the respective screen.

Many thanks!
Rachel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic