0

I have a class that is handling printing the various messages into the console, lets call this class ConsoleMessages.java. This class is public and abstract and all its methods are public and static.

I want to make an interface to this class (lets call it PrintMessages). I mean so, that ConsoleMessages.java will implement PrintMessages.

The thing is, JAVA doesn't support static methods in an interface.

What would you advise me to do?

3
  • 1
    What makes you think you want static methods on an interface? Please explain what you're trying to accomplish. Commented Aug 1, 2011 at 17:49
  • Since you call those methods on the class, not its instances, I don't think static methods are required to be in the interface. Commented Aug 1, 2011 at 17:50
  • You're going about it backwards. Define the interface then implement the functionality. What you're doing sounds all sorts of wrong and isn't likely to be very testable or maintainable in the long run. What is it you're trying to accomplish? Perhaps we can recommend an alternative design. Commented Aug 1, 2011 at 17:51

4 Answers 4

3
  • Create the PrintMessages interface with the methods you desire.

  • Make ConsoleMessages a class that implements that interface. Change all methods from static to non static.

  • Enforce ConsoleMessages instantiation as a singleton. This can be achieved in many ways, either doing it yourself or using a Dependency Injection framework.

Sign up to request clarification or add additional context in comments.

Comments

1

There is really no strong arguement against static methods in interface. Nothing bad would happen.

An interface can have static fields and static member classes though, therefore static methods can be attached through them, albeit with one extra indirection.

interface MyService static public class Factory static public MyService get(...) return ...; MyService service = MyService.Factory.get(args); 

2 Comments

An advantage of this approach is that you could achieve the effect of a static import by implementing an interface. The advantage of doing it this way over just using a static import is that the import affects the entire compilation unit. In contrast this approach just affects the one class - which could be a private class nested inside a top level class.
In both Java and .NET, there are a number of situations where methods which exist entirely for the purpose of working with one particular interface end up having to be static members of some other class, when it would have been cleaner to have them be members of the interface itself. I think Java 8 will improve things, though I've not used it yet.
0

If you find yourself needing to define interfaces on a utility class then it may be time to revisit your design choices. Your ConsoleMessages class seems to have outgrown its initial use as a dumping ground for 'common utility functions'.

Short answer? Refactoring time.

Comments

-1

Interfaces are there to specify methods for objects (which will then be implemented by some class). You have no objects here, thus you need no interface.

Static methods can only be called using the exact class name (or alternatively the name of some subclass), there is no point in using an interface to do this.

So, you have two options:

  • Throw your interface away and stay with the static methods.
  • Make all methods (or at least these which should be in the interface) non-static, and your implementing class non-abstract. To call them one then would need an object of the class (implementing this interface).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.