private

Java’s javap command doesn’t show private members (by default)

As I was just reminded, Java’s javap command doesn’t show private members by default. You have to use the -p option of javap to see private members.

I was just reminded of that when using the Scala REPL. Given this Person class with a private constructor field named name:

class Person(private var name: String)

javap without the -p option shows this:

How to control Scala method scope with private, package, and more alvin June 13, 2015 - 7:10pm

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

Problem

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

Back to top

Solution

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 prevent getter and setter methods from being generated in Scala classes

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.7, “How to prevent getter and setter methods from being generated in Scala classes.”

Problem

When you define a class field as a var, Scala automatically generates getter and setter methods for the field, and defining a field as a val automatically generates a getter method, but you don’t want either a getter or setter.

How to define a private primary constructor in Scala alvin June 13, 2015 - 1:48pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.4, “How to define a private primary constructor in Scala.”

Problem

You want to make the primary constructor of a Scala class private, such as to enforce the Singleton pattern.

Solution

To make the primary constructor private, insert the private keyword in between the class name and any parameters the constructor accepts:

How to control the visibility of Scala constructor fields alvin June 13, 2015 - 1:38pm

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.”

Problem

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

Scala method and function examples alvin September 30, 2014 - 9:52am

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.)

Scala class examples (constructors, case classes, parameters) alvin September 28, 2014 - 6:53pm

This post contains a collection of examples of Scala classes and class properties. I created most of these in the process of writing the Scala Cookbook. Unlike the Cookbook, I don’t describe them much here, I just show the examples, mostly as a reference for myself (and anyone else that can benefit from them).

Scala FAQ - How to create a private default/primary constructor alvin June 5, 2012 - 7:04am

I haven't had the need for this syntax yet, but I just saw some code that shows how to create a private primary constructor in Scala, and I thought I'd test it and share an example of it.

In the example below, I've made the primary constructor of the Order class private:

What Twitter icons mean

Twitter icons meaning: I may be the only one on the planet who doesn't understand the meaning of the current Twitter icons, but really, I don't understand most of them. A Twitter reply icon is an empty ballon? The media icon I kinda get, but the Twitter geo pin icon I don't get. (Maybe I don't work with geo tags and geographic locations much, but I can think of a few million people who also don't.)

How do I make variables private to my Perl function? alvin August 28, 2008 - 3:02pm

Perl function FAQ: How do I make variables private to my Perl function?

Answer: Just use the Perl my operator. Here's an example that shows how to create a variable named bar that is private to the function name foo: