Java Introduction to Scala Functional Programming with Scala Classes and Constructors

Jimmy Li
Jimmy Li
126 Points

What does "hulk.age = 30" mean exactly at 6:30 in the video?

Andi sets the age of "hulk", an instance of SuperHero, to 30 at 6:30 in the video. I am wondering what "hulk.age = 30" means specifically.

Is he directly setting a variable called "age" in the SuperHero class to 30? If so, why didn't he set the variable called "heroAge" to 30 instead?

Or is he calling the setter for age that we defined as "age_=" earlier? If so, does that mean that ".age" is the syntax to call the setter "age_=" in Scala?

1 Answer

Balázs Góczán
Balázs Góczán
22,698 Points

There is a field named heroAge in the SuperHero class.

def age_=(newAge: Int): Unit = {

This line is the first line of the setter, because setters in scala are the field name followed by an underscore.