• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interfaces and Static and design advice request

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic