Q:

How to create private fields that my functions will return

My class currently looks like:
class WebConfig(config: Config) {

  def this() {
     this(ConfigFactor.load())
  }

  def dbPort = config.getInt("mysql.port")

}

I don't like that when I call dbPort, it has to call and then cast the config each and every time.
So I want to create private fields and set them in the constructor, so then calling dbPort will simply return what a private field has.
How can I do this?
I tried creating a private var but I am getting this error:
 class WebConfig(config: Config) {
      private var _dbPort: Int
      def this() {
         this(ConfigFactor.load())

         _dbPort = config.getInt("mysql.port")
      }

      def dbPort: Int = _dbPort

    }

Error:
abstract member may not have private modifier

A:

Can't you just write this:
class WebConfig(config: Config) {

  def this() {
     this(ConfigFactor.load())
  }

  val dbPort = config.getInt("mysql.port")

}

It will only read the config parameter one time.

