0

I'd like to do the following: Given a Java Class A, which is part of a Java only project, I'd like to write an accompanying object to it in a separate file, so that scala code can use the A() syntax for the apply method. Is there a way to do this? I've tried the following:

// in A.java 
public class A{
  private final int i;

  public A(int i){
    this.i = i;
  }
}

// In a separate file A$.java in the same package: 
   public class A${
    public static A apply(int 3){ return new A(3);}
 }


//Scala code, in S.sca
 class S { 
   val a = A(42)   //Error:  object A is not a value
 }

Writing an object A directly in Scala doesn't work either. Is there a way to achieve this, passing some args to the compiler or so?

Chirlo
  • 5,989
  • 1
  • 29
  • 45
  • 2
    [In Scala, how can I define a companion object for a class defined in Java?](http://stackoverflow.com/questions/4921827/in-scala-how-can-i-define-a-companion-object-for-a-class-defined-in-java) – Sergii Lagutin Nov 05 '14 at 13:07
  • Damn, searching for "accompanying object" returned nothing useful, should have searched for "companion object". Thanks! – Chirlo Nov 05 '14 at 13:25
  • 1
    (btw, "Companion Object" is the correct nomenclature. You may want to stop calling it "Accompanying object" ;) – Diego Martinoia Nov 05 '14 at 14:33

0 Answers0