开发者

Accessing scala object fields from java

开发者 https://www.devze.com 2023-04-13 05:12 出处:网络
I\'m having trouble access开发者_开发百科ing the fields of a scala object from java. Scala: object TestObject {

I'm having trouble access开发者_开发百科ing 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;


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();
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消