• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

For classes with multiple constructors, which to choose as primary constructor?

 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just swapped out a Java class in our code base for a Scala one. I ran into the following "problem." The Java class was basically a POJO with three constructors. There was a no-args constructor, a constructor with one arg, and a constructor with 22 args matching the 22 members of the class. The Scala approach that seems to be the most idiomatic is to choose the 22 arg constructor for the primary constructor. This allows (since most members were vals) the other constructors to call something like:



The thing, though, is that I don't like the readability here. "This" followed by 22 parameters most of which are null or false in this case seems like a recipe for future errors and it is impossible to tell easily what value is being passed for what member.

Is it better to make the no-arg constructor the primary? If so, with all the variables being vals, how do you create an auxiliary constructor that can write to val's from the auxiliary constructor? I think that trying to call this() and then trying to reassign to the val will trigger a compiler error.

Is there something else I'm not thinking of, that would make this code more maintainable?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Scala, the primary constructor is always called. Extra constructors must always contain a 'this(...)' call to call the primary constructor. So you must choose the constructors in such a way that this works out.

You cannot reassign a val - a val can be initialized once, and its value cannot be modified.
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper. This is such a strange language but a very cool one. What I found worked out for me was to use a combination of default parameters and named parameters.

Thus in the auxiliary constructors, when I called the default constructor, I could leave out passing all 22 arguments and focus on only those that were different.

Using fake code to illustrate:

reply
    Bookmark Topic Watch Topic
  • New Topic