Scala Constructors (original) (raw)

Last Updated : 11 Aug, 2021

Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation.
Scala supports two types of constructors:

Primary Constructor

When our Scala program contains only one constructor, then that constructor is known as 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... }

Important points:

// Scala program to illustrate the // concept of primary constructor

// Creating a primary constructor // with parameter-list class GFG(Aname: String, Cname: String, Particle: Int) { def display() { println("Author name: " + Aname); println("Chapter name: " + Cname); println("Total published articles:" + Particle); } }

object Main { def main(args: Array[String]) {

    // Creating and initializing
    // object of GFG class
    var obj = new GFG("Ankita", "Constructors", 145);
    obj.display();
}

}

`

Output:

Author name: Ankita Chapter name: Constructors Total published articles:145

// Scala program to illustrate the // concept of default primary constructor

class GFG { def display() { println("Welcome to Geeksforgeeks"); } }

object Main { def main(args: Array[String]) {

    // Creating object of GFG class
    var obj = new GFG();
    obj.display();
}

}

`

Output:

Welcome to Geeksforgeeks

// private constructor with two argument class GFG private(name: String, class:Int){ // code.. }

// private constructor without argument class GFG private{ // code... }

// Scala program to illustrate the // concept of primary constructor

// Creating primary constructor with default values class GFG(val Aname: String = "Ankita", val Cname: String = "Constructors") { def display() { println("Author name: " + Aname); println("Chapter name: " + Cname);

}

}

object Main { def main(args: Array[String]) { // Creating object of GFG class var obj = new GFG(); obj.display(); } }

`

Output:

Author name: Ankita Chapter name: Constructors

Auxiliary Constructor

In a Scala program, the constructors other than the primary constructor are known as auxiliary constructors. we are allowed to create any number of auxiliary constructors in our program, but a program contains only one primary constructor.

Syntax:

def this(......)

Important points:

Example:

Scala `

// Scala program to illustrate the // concept of Auxiliary Constructor

// Primary constructor class GFG( Aname: String, Cname: String) { var no: Int = 0;; def display() { println("Author name: " + Aname); println("Chapter name: " + Cname); println("Total number of articles: " + no);

}

// Auxiliary Constructor
def this(Aname: String, Cname: String, no:Int) 
{
    
    // Invoking primary constructor
    this(Aname, Cname)
    this.no=no
}

}

object Main { def main(args: Array[String]) {

    // Creating object of GFG class
    var obj = new GFG("Anya", "Constructor", 34);
    obj.display();
}

}

`

Output:

Author name: Anya Chapter name: Constructor Total number of articles: 34