Why can not I define an interface with static methods?

13

I would like to force some of my classes to be implemented in Singleton, but I came across the following situation.

interface ICharacterSingleton{
    static Characters getInstancia();
}

public static class Zero extends Characters implements ICharacterSingleton {        
    private static Characters Instancia = null;
    private Zero(){     
        Layout.add("   111  ");
        Layout.add("  1   1 ");
        Layout.add("  1   1 ");
        Layout.add("  1   1 ");
        Layout.add("  1   1 ");
        Layout.add("  1   1 ");
        Layout.add("  1   1 ");
        Layout.add("   111  ");                         
    }

    public static Characters getInstancia() {
        if(Instancia == null)
            Instancia = new Zero();         
        return Instancia;
    }               
}

Can not I define a static method for an interface? is there another way out?

    
asked by anonymous 18.07.2014 / 00:07

6 answers

8

I made my comment in response due to size.

The problem seems to be perfectly solved by linking the concept of the Factory Method design pattern with Singleton , that is, a method that makes a single instance of a class.

Below I'll put two possible implementations ...

Approach # 1: one method and one attribute for each class

public abstract SingletonFactory {

    private static Zero zero = new Zero(); 
    public static Zero getInstanciaZero() {
        return zero;
    }

    private static Um um = new Um(); 
    public static Um getInstanciaUm() {
        return  um;
    }

    //...

}

Approach # 2: a generic method that receives the number as a parameter

public abstract SingletonFactory {

    private static Map<Byte, ICharacterSingleton> numberMap = new HashMap<>(); 

    static {
        numberMap.put(0, new Zero());
        numberMap.put(1, new Um());
        //...
    }

    public static ICharacterSingleton getInstancia(Byte numero) {
        return numberMap.get(numero);
    }

}
    
18.07.2014 / 20:48
12

Methods in interfaces are implicitly public and abstract .

That is, however much you declare such a method on your interface:

public interface MinhaInterface {
    void teste();
}

Implicitly what you are doing is:

public interface MinhaInterface {
    public abstract void teste();
}

So the problem is not the interface, the problem is the abstract f modifier that conflicts with the static modifier. For example, you can not do this for a class either:

public abstract class MinhaClasse {
    public abstract static void teste();
}

The error is the same as trying to create a static method on an interface. The reason for these two modifiers to be conflicting is simple: if the method is static it means that it belongs to the class, however if it is abstract means it has no implementation, so what are you going to access a method of a class that does not have implementation ?

Basically:

  • static prevents you from overwriting the method, if a subclass uses the same signature as a static method, then this subclass is redefining the method and not overwriting; while

  • abstract forces the first concrete subclass to implement such a method, by means of the superscript.

So they are conflicting.

The solution to your case, I will think a little more, but at first I would say that it is not possible by contract to have a singleton class.

    
18.07.2014 / 00:37
4

The interesting thing about interface is being able to separate the class implementation from the "contract" that says what it has. That way, I can get an object that I know implements the interface and work with it without knowing what your class is:

IEnumeravel obj = ...;
obj.BuscaEnumerador(); 

In the small example above I do not know the obj class, can be every one hour, can be received as parameter. The contract is made so that I know that the object that arrives will have the method for me to call.

static methods are called by the class, I need to know that I'm working with the class Zero to call the method:

Zero.getInstancia();
    
18.07.2014 / 00:20
2

Because it would contradict how Java does to invoke a static method.

A state-independent and instinct method, correct? Therefore, it can be run directly from the ".class".

Now, if you could have a static method in an interface, Java would have to look for a particular implementation of the interface in order to execute the method.

However, it could let you do something like:

public interface Coisa {
  public static int f() {
    // ...
  }
}

But you can not: P

    
18.07.2014 / 00:20
1

There are essentially two types of methods: instance methods and static (class) methods.

This concept belongs to the object-oriented paradigm, not peculiar to any language.

Because static methods belong to the class, when we create a static method in a class, when we start the program, in the loader class, we check which classes have static methods. When a class with a static method is identified, the implementation of this method is placed in a global scope, which can be accessed through the type that holds the static class.

Why can not implement static methods in interfaces.

Because the loader class looks for an implementation to put it in a global scope, it is not allowed to place static methods on interfaces, because interfaces do not implement methods, they signatures.

Therefore, it is not possible to implement static methods in interfaces.

    
18.07.2014 / 00:39
-2

It is not possible because an interface can not be instantiated as a concrete class or it has no constructor method, when it attempts to access a static method internally the class is instantiated and is ready to have direct access to that constructor method. p>

[] 's

    
20.07.2014 / 23:19