15

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;
1
  • To see all the public members use: javap -classpath <classpath> <classname>. E.g. javap -classpath target/scala-2.9.1.final/classes TestObject Commented Oct 17, 2011 at 1:15

1 Answer 1

17

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();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.