aspose file tools*
The moose likes Java in General and the fly likes Interfaces and Static and design advice request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interfaces and Static and design advice request" Watch "Interfaces and Static and design advice request" New topic
Author

Interfaces and Static and design advice request

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jul 04, 2003
Posts: 1312
Why can one not do the following?

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



Because method of interface MUST have public and abstract only.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
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

Joined: Mar 18, 2003
Posts: 281
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


The nice thing about Standards is that there are so many to choose from!
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces and Static and design advice request