1

Possible Duplicate:
Accessing scala object fields from java

I have this scala object

object Foo {
  val TOTAL = 10
  def totalMethod = 10
}

When I do this on java code:

System.out.println(Foo.totalMethod());

it works, but when I do

System.out.println(Foo.TOTAL);

It doesn't:

    [error] /Users/pfernand/Projects/foo/Whatever.java:23: cannot find symbol
    [error] symbol  : variable TOTAL
    [error] location: class Foo
    [error]         System.out.println(Foo.TOTAL);
    [error]     
1
  • It's a duplicate indeed. Sorry, but I couldn't find the answer by googling :( Commented Aug 9, 2012 at 16:38

3 Answers 3

4

The Scala compiler generates a parameterless method with the name of the field. You can use

System.out.println(Foo.TOTAL());
Sign up to request clarification or add additional context in comments.

Comments

2

Try

System.out.println(Foo.TOTAL());

Scala makes fields private and provides a "getter" method (using the same name) for access.

Comments

1

The above will create a getter, which you should be able to access with Foo.TOTAL(). There's no way to access the field directly from Java, AFAIK.

1 Comment

That's right, the field is private.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.