Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
23,669 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.