smartnut007
smartnut007

Reputation: 6423

Accessing Scala standalone singleton object from Java

I am having trouble accessing the method fromString in Scala. I tried javap and at least one of the ways i am accessing should be working, but its not.

App.scala

object App { 
 def fromString(s:String) : Option[Int] =  if( s == "0" ) Some(0) else None
}

Test.java

public class Test {
    public static void main(String[] args){
           //THEY ALL GIVE COMPILER ERRORS
           //App.fromString("");
           //App$.fromString("") ;
           //App$.MODULE$.fromString("");

    }
}

JAVAP OUTPUTS

javap App Compiled from "App.scala"

public final class App extends java.lang.Object{
    public static final scala.Option fromString(java.lang.String);
}

javap App$ Compiled from "App.scala"

public final class App$ extends java.lang.Object implements scala.ScalaObject{
    public static final App$ MODULE$;
    public static {};
    public scala.Option fromString(java.lang.String);
}

Upvotes: 0

Views: 1014

Answers (1)

Luigi Plinge
Luigi Plinge

Reputation: 51109

I just tried it in Eclipse, and the expected

App.fromString("");

compiles fine. Eclipse shows an error "App cannot be resolved", but I've learnt to ignore Eclipse's errors.

Maybe you haven't put your classes in the same package?

Upvotes: 1

Related Questions