Scala | Primary Constructor
Last Updated :
29 Jul, 2021
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
Scala
// 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
// 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
// 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
// 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.
Similar Reads
Scala Constructors 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 c
4 min read
Scala Constructs Some of the basic Scala constructs are expressions, blocks, classes, objects, functions, methods, traits, Main methods, fields, and closures. 1. Scala Expression: A computable statement in Scala is known as expression. For example, the following is an expression: 3 + 4. To print the output of an exp
5 min read
Scala | Auxiliary 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. In Scala Program, the constructors other than the primary constructor are known as Auxiliary Con
3 min read
Constructors in Python In Python, a constructor is a special method that is called automatically when an object is created from a class. Its main role is to initialize the object by setting up its attributes or state. The method __new__ is the constructor that creates a new instance of the class while __init__ is the init
3 min read
Calling A Super Class Constructor in Scala Prerequisite - Scala ConstructorsIn Scala, Constructors are used to initialize an object's state and are executed at the time of object creation. There is a single primary constructor and all the other constructors must ultimately chain into it. When we define a subclass in Scala, we control the sup
3 min read