| 1. |
How Does It Work Under-the-hood, When We Create An Instance Of A Class Without Using ‘new’ Keyword In Scala? When Do We Go For This Approach? How To Declare Private Constructors In Scala? |
|
Answer» In Scala, when we CREATE an instance of a Class without using ‘new’ KEYWORD, internally it make a call to appropriate apply method available in Companion OBJECT. Here appropriate apply method means that MATCHED with parameters. When do we choose this option: When we need to provide private private constructor and we need to avoid using ‘new’ keyword, we can implement only apply method with same SET of parameters and allow our class users to create it without new keyword. In Scala, when we create an instance of a Class without using ‘new’ keyword, internally it make a call to appropriate apply method available in Companion object. Here appropriate apply method means that matched with parameters. When do we choose this option: When we need to provide private private constructor and we need to avoid using ‘new’ keyword, we can implement only apply method with same set of parameters and allow our class users to create it without new keyword. |
|