Open In App

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 –  



Let’s understand it better with some examples.

Example #1: A primary constructor with parameter-list  




// 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 initializing
        // object of GFG class
        var obj = new GFG("Scala", "Constructors", 14);
        obj.show();
    }
}

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.




// 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();
    }
}

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 constructor with default values




// Scala program to illustrate the
// concept of primary constructor
 
// Creating primary constructor 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();
    }
}

Output: 
 

Language name: Scala
Topic name: Constructors

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




// 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)
}

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. 
 


Article Tags :