Scala | Primary Constructor

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. When our Scala program contains only one constructor, than that constructor is called a primary constructor.
The primary constructor and the class share the same body, means we need not to create a constructor explicitly.

Syntax:

class class_name(Parameter_list) {
// Statements...
}

Some important points about primary constructor in Scala –

  • The primary constructor can have zero or more parameters.
  • The parameters of parameter-list are declared using var within the constructor then the value could change. Scala also generates getter and setter methods for that field.
  • The parameters of parameter-list are declared using val within the constructor then the value cannot change And Scala also generates only a getter method for that field.
  • The parameters of parameter-list are declared without using val or var in the constructor, then the visibility of the field is very compact and Scala does not generate any getter and setter methods for that field.
  • The parameters of parameter-list are declared using private val or var in the constructor then it prevents from generating any getter and setter methods for that field. So, these fields can be accessed by the members of that class.
  • In Scala, only a primary constructor is allowed to invoke a superclass constructor.

Let’s understand it better with some examples.

Example #1: A primary constructor with parameter-list

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to illustrate the 
// concept of primary constructor 
  
// Creating a primary constructor 
// with parameter-list 
class GFG(Lname: String, Tname: String, article: Int) 
    def show() 
    
        println("Language name: " + Lname); 
        println("Topic name: " + Tname); 
        println("Total published articles:" + article); 
    
  
// Creating object
object Main 
    // Main method
    def main(args: Array[String]) 
    
          
        // Creating and initialzing 
        // object of GFG class 
        var obj = new GFG("Scala", "Constructors", 14); 
        obj.show(); 
    

chevron_right


Output:

Language name: Scala
Topic name: Constructors
Total published articles:14

In above example Lname, Tname and article are the parameter of the primary constructor and display is the function to print values.
 
Example #2: A primary constructor with parameter-list.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to illustrate the 
// concept of default primary constructor 
  
class GFG 
    def show() 
    
        println("Welcome to Geeksforgeeks"); 
    
  
// Creating object
object Main 
    // Main method
    def main(args: Array[String]) 
    
          
        // Creating object of GFG class 
        var obj = new GFG(); 
        obj.show(); 
    

chevron_right


Output:

Welcome to Geeksforgeeks

In above example, we can see compiler will automatically create a primary constructor when we create an object of our class, this constructor is known as a default primary constructor.
 
Example #3: Primary construtor with default values

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to illustrate the 
// concept of primary constructor 
  
// Creating primary construtor with default values 
class GFG(val Lname: String = "Scala"
        val Tname: String = "Constructors"
    def show() 
    
        println("Language name: " + Lname); 
        println("Topic name: " + Tname); 
          
    
  
// Creating object
object Main 
    // Main method
    def main(args: Array[String]) 
    
        // Creating object of GFG class 
        var obj = new GFG(); 
        obj.show(); 
    

chevron_right


Output:

Language name: Scala
Topic name: Constructors

 
Example #4: Primary constructor private by using a private keyword.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to illustrate the 
// concept of primary constructor 
// by using private keyword
class GFG private
{
    // Define method
    override def toString = "Welcome to GeeksForGeeks."
}
  
// Creating object of class GFG
object GFG 
{
    // Creating object    
    val gfg = new GFG
    def getObject = gfg
}
  
object SingletonTest extends App 
{
  
  // this won't compile
  // val gfg = new GFG
  // this works
  val gfg = GFG.getObject
  println(gfg)
}

chevron_right


Output:

Welcome to GeeksForGeeks.

As we can see in above example, private keyword is used in between the class name and the constructor parameter-list. val gfg = new GFG (this line of code) won’t even compile because the primary constructor is private.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.