How to control Scala method scope with private, package, and more

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 5.1, “How to control method scope in Scala (private, package, more).”

Back to top


Scala methods are public by default, and you want to control their scope in ways similar to Java.

Back to top


Scala lets you control method visibility in a more granular and powerful way than Java. In order from “most restrictive” to “most open,” Scala provides these scope options:

Table of Contents

  1. Problem
  2. Solution
Back to top

How to control the visibility of Scala constructor fields

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.2, “How to control the visibility of Scala class constructor fields.”


You want to control the visibility of fields that are used as constructor parameters in a Scala class.

Scala method and function examples

This page contains a collection of Scala method examples. I created many of these examples while I was writing the Scala Cookbook. Unlike the Cookbook, where I explain these examples in great detail, on this page I’m just sharing many of the examples so you can use this as a method/function reference page. (The Cookbook contains more examples than this page, and explains them in detail.)

Free Public WiFi

While traveling the other day I was looking for some free wireless internet access, and when I looked in the Wireless/AirPort menu on my MacBook Pro I noticed the usual listing of publicly broadcasting wi-fi networks in the top part of the menu, but then under the "Devices" portion of the menu I noticed an entry labeled "Free Public WiFi". For a brief moment that sounded like what I needed, but then the alarms in my head went off, as it sounded more like a trap, something like the typical worm/virus/hacker/phishing setup.