Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7873371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:29:39+00:00 2026-06-03T02:29:39+00:00

A quite simple exercise from Cay Horstmann’s book « Scala for the impatient »

  • 0

A quite simple exercise from Cay Horstmann’s book « Scala for the impatient » keeps puzzling me. It’s about primary,auxiliary and default primary constructors :

ex 5.10 :
Consider the class

class Employee(val name: String, var salary: Double) {
  def this() { this("John Q. Public", 0.0) }
}

Rewrite it to use explicit fields and a default primary constructor.

I’m not sure about I am supposed to do. Could some of you propose a solution ?

However, trying to solve this exercise may have made me aware of something I hadn’t noticed before about primary constructor and val fields (as you can see, I’m not quite sure) :

Am I right if I say that a val field (as name in the Employee class above) may only be initialized through the primary constructor and not by an auxiliary one ? In the latter case it would be considered by the compiler as a reassignment to a val field causing an error.

At first I thought to val fields as a rough equivalent to final fields in java expecting that it would be legal to assign them for the first time in any constructor but it seems I was wrong.

I am not quite satisfied with what may only be a wild guess so I would appreciate if someone could give me more accurate information about that point.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:29:41+00:00Added an answer on June 3, 2026 at 2:29 am

    From “Programming in Scala, 2nd edition” paragraph 6.7:

    In Scala, every auxiliary constructor must invoke another constructor of the same class as its first action.
    The net effect of this rule is that every constructor invocation in Scala will end up eventually calling the primary constructor of the class. The primary constructor is thus the single point of entry of a class.

    So all data for initialization of val must be in primary constructor.

    Your class with explicit fields may be something like:

    class Employee(n: String = "John Q. Public", s: Double = 0.0) {
      val name = n
      var salary = s
    }
    

    or without default parameter values:

    class Employee(n: String, s: Double) {
      def this() = this("John Q. Public", 0.0)
      val name = n
      var salary = s
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem is probably quite simple. $q = SELECT id FROM users WHERE name
I'm considering porting a very simple text-templating library to scala, mostly as an exercise
A quite simple FOR INSERT trigger keeps returning the error Subquery returned more than
This should be quite simple, but I can't work out the syntax. I have
It's quite simple, I don't understand why the following does match: preg_match('/\<td valign=top class=bericht\>(.*\s)*<\/td>/',$html,$matches3);
My question is quite simple.. I need to convert an Element object into an
My schema is quite simple- Each user has an id and name. There are
My question is quite simple. Is it possible to inactivate all toasts of an
My issue is quite simple I think : I would like to be able
The question is quite simple : I have a controller plugin for Zend Framework.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.