Java interface- static method call

I got a problem I can't wrap my head around, and hope somebody can help me. Essentially, I have a hashed message and a signature. To verify it, I have to call a method in another class, with Signing.verify(hash, signature). The interface, also used for other signatures, hence it cannot be changed, forbids static methods, but Java tells me either the method becomes static or I cannot verify it. I'm rather new to Java, but I hope somebody can help me. If the problem has already been encountered, I am sorry, but I have not found anything. Yours sincerely, and thank you for your time.

EDIT: Sorry-this is kind of complicated. The method, in the same package, looks like this- and is to be programmed later:

public class Signature implements SIG{
    public boolean verify(String message, String signature){
        //deadcode
        return true;
    }
}

My code looks like this:

public class SignatureTree implements Tree{
    public boolean verify(String message, String signature, String hash){
            byte[] publicKey=extractPK(signature);
            if(Signature.verify(message,signature)==false){
                   System.err.println("SIGNATURE VERIFICATION FAILED");
            }
            //some other stuff
    }
}

However, Eclipse is telling me to change the verify method in Signature to a static method-which is forbidden by the interface. Is that any more clear? Sorry-I'm more of a C programmer. Thank you very much.

2 answers

  • answered 2017-01-11 14:22 Denny R

    Question still not 100% clear, but from what I can understand, you should find another class that implements that interface.

    Another option, long shot, probably won't work:

    if(new Signature().verify(message,signature) == false) { // fail code here }

    That code would assume Signature is not an interface, though.

  • answered 2017-01-11 14:22 SimMac

    You may want to read about Classes and their members in Java: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

    See, a class is something like a "template for objects", and you can instantiate an object of a certain class. For example, let's create a simple class:

    class MyClass {
        public int var;
    
        public MyClass(int var) {
            this.var = var;
        }
    
        public void incrementVar() {
            var = var + 1;
        }
    
        public static boolean isSmallerThanTen(int toTest) {
            return (toTest < 10);
        }
    }
    

    Now you can create objects that are instances of MyClass:

    MyClass firstObject = MyClass(3);
    MyClass secondObject = MyClass(10);
    

    And you can call the incrementVar() method on a single object:

    firstObject.incrementVar();
    

    Now, firstObject.var is 4, but the second object has not been touched, secondObject.var is still 10.

    But then, you have this method isSmallerThanTen() which is independent of the properties of a certain instance, you can call MyClass.isSmallerThanTen(5). However, you can not call MyClass.incrementVar(), because incrementVar() is not static, it can only be called on an instance of MyClass, not on the class itself.

    And that's exactly the problem you have, Signature.verify() is not static, which means you have to call it on an instance of Signature, not on the class itself.

    So the following two solutions should both work, as they both instantiate an object of Signature:

    if(new Signature().verify(message,signature) == false){
        ...
    

    and

    Signature sig = new Signature();
    if(sig.verify(message, signature) == false) {
        ...
    

    Btw: You can also write this as if(!sig.verify(message, signature)), the ! is a logical "not", inverts the boolean value.