Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements (i.e. instructions). Statements are executed at the time of object creation. In Scala Program, the constructors other than the primary constructor are known as Axiliary Constructors. We are allowed to create any number of auxiliary constructors in our Scala class, but a scala class contains only one primary constructor.
Auxiliary constructors are defined as methods in the class with the keyword this. We can describe multiple auxiliary constructors, but they must have different parameter lists.
Let’s try to understand Auxiliary constructors with help of some examples.
Example #1: Using one Auxiliary Constructor
Language name: Scala Topic name: Constructor Total number of articles: 4
In above example, as we can see only one auxiliary constructor is used and primary constructor invoked in that auxiliary constructor. After creating object of GFG class(obj),
show() function will be called and print the result.
Example #2: Using more than one Auxiliary Constructor.
Language name: Total number of employee: 0 Language name: GeeksForGeeks Total number of employee: 0 Language name: GeeksForGeeks Total number of employee: 42
In above example, as we can see two auxiliary constructors are created with different parameters. Auxiliary constructor invoked primary constructor and another auxiliary constructor invoked previously defined auxiliary constructor.
Some Important Points About Auxiliary Constructor
- In a single class, we are allowed to create one or more than one auxiliary constructors, but they have different signatures or parameter-lists.
- Each auxiliary constructor must call one of the previously defined constructors, this would be primary constructor or previous auxiliary constructor.
- The invoke constructor may be a primary or previous auxiliary constructor that comes textually before the calling constructor.
- The first statement of the auxiliary constructor must contain this keyword.
- Scala | Primary Constructor
- Calling A Super Class Constructor in Scala
- Scala | Controlling visibility of constructor fields
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
- Program to convert Java list to an iterator in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.