Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing scala object fields from java

I'm having trouble accessing the fields of a scala object from java.

Scala:

object TestObject {
  val field = 5 
}

Java:

public class JavaTest
{
  public static void main(String[] args)
  {
    int i = TestObject.field;
  }
}

Error:

[error] JavaTest.java: cannot find symbol
[error] symbol  : variable field
[error] location: class TestObject
[error]     int i = TestObject.field;
like image 939
dsg Avatar asked Oct 17 '11 00:10

dsg


1 Answers

Scala fields are private variables with a getter of the same name to preserve immutability.

public class JavaTest
{
  public static void main(String[] args)
  {
    int i = TestObject.field();
  }
}
like image 197
dsg Avatar answered Oct 16 '22 10:10

dsg