How to access Scala var and val fields from Java

I just ran into one thing I wish I had included in the Scala Cookbook that I didn’t include: How to access a val or var field in a Scala object from your Java code.

In short, if you have a field named appName defined in a Scala object, like this:

val appName = "Little Logger"

and you want to access it from your Java code, you’ll write your Java code like this:

Global.appName();

As you can see, you need to access the field as a “getter” method. That’s because Scala generates getter methods for public val fields, and getter and setter methods for public var fields, and you need to access the Scala fields using these generated methods.