How to implement Scala apply method in Java

Viewed 2115

I want to call some Java code from Scala code. I would like to use Scala's apply construct, so I can call it like this:

val led = OutPin(0)

instead of:

val led = new OutPin(0)

I naively implemented an additional apply method in my Java code like this:

public class OutPin {

    public OutPin(int pinNumber) {
    }

    public OutPin apply(int pinNumber) {
        return new OutPin(pinNumber);
    }
}

This does not make my Scala code (first line above) compile, and instead gives me an error:

Object OutPin is not a value

What is the correct way to implement Scala's apply method in Java?

3 Answers
Related