0

I have a Java method of the form

    public interface JavaInterface< T extends A >{
        static < T extends A > JavaInterface< T > callThis(){
         //I want to call this in scala

        }
    }

In Scala I write

val x = JavaInterface[SomeClass].callThis()

but I get an error telling me it "is not a value". How do I call that static method in Scala?

2
  • You suppose to have static methods in the companion object which will be available for you like in Java. ClassName.methodname Commented Nov 13, 2020 at 10:00
  • 2
    I think he wants to call that Java method from Scala code. Please provide a full example of what you're doing, including the full error message you're getting. Commented Nov 13, 2020 at 10:16

2 Answers 2

6

You want:

val x = JavaInterface.callThis[SomeClass]()

It's the method, not the type, that's parameterised for static methods.

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

Comments

1

The code you have now assumes JavaInterface is an object with a nilary apply method that returns another object with a callThis() method. For that, your code would have to look something like this (in Scala):

trait JavaInterface[T] {
  def callThis() = println("foobar")
}

object JavaInterface {
  def apply[T <: JavaInterface[T]] = new JavaInterface[T] {}
}

Since you are calling the callThis method on JavaInterface, you need to do JavaInterface.callThis[SomeClass]() instead, giving the type parameter to the method instead of the object (or Java interface) you're calling it on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.