Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Scala - Primary Constructor - Testingpool

Scala – Primary Constructor

In the previous chapter, we have learnt about package object in scala. Now, we will see constructor and its types in scala.

Constructor is used for creating an instance (object) of a class. Scala has 2 types of constructor and it works in a different way than constructor in Java.

  1. Primary Constructor
  2. Auxiliary Constructor

There are few points that we need to remember as follows.

  • A scala class can have only one primary constructor but any number of auxiliary constructors.
  • A scala class can have either primary constructor or both primary and auxiliary constructors.
  • Not a special method, a constructor is different in Scala than in Java constructors.

We will see both primary and auxiliary constructor in details one by one.

Primary Constructor:

We we create a class, compiler creates a constructor and it will be called automatically while an object is created with that class. A primary constructor can have zero or one or more parameters. Primary constructor with no parameters is also called default constructor. All statements defined inside the class body will be treated as part of the constructor.

Example: Suppose we have a class named as Employee with no parameters. A default constructor will be called when class instance(object) is created.

package main.com.testingpool

object ConstructorExample extends App{
  val emp1 = new Employee()      //1st - creating instance of class
  val emp2 = new Employee        //2nd way -creating instance of class
}


class Employee{    //class definition
  // class body

  println("Hey!, I am a default constructor.")
}
Output:
Hey!, I am a default constructor.
Hey!, I am a default constructor.

Primary constructor with parameters:

In the below example, Employee class has three parameters named as firstName, middleName and lastName. Some employees will have middle name and some will not, but we have to pass all three arguments each time we create an object. This is not ideal practice, but it can overcome by using auxiliary constructor which will see in next chapter.

package main.com.testingpool

object ConstructorExample extends App{
  val emp1 = new Employee("Shekhar","", "Sharma")
}


class Employee(firstName:String,middleName:String,lastName:String){
  // class body

  println(firstName +" " + middleName + " "+ lastName )
}
Output:
Shekhar  Sharma

As stated above, all statements like if, while, for loop, function call etc. defined in the class body become part of constructor, except method definition. You can understand with the help of below example.

package main.com.testingpool

object ConstructorExample extends App{
  val emp1 = new Employee("Shekhar","", "Sharma")
}


class Employee(firstName:String,middleName:String,lastName:String){
  // class body
  println("First Statement")       //print statement
  def fullName = firstName +" " + middleName + " "+ lastName //method definition
  fullName      //method calling

  if(middleName.equals("")){
    println("middle name is empty.")
  }

  println("Last Statement")      //print statement

}
Output:
First Statement
middle name is empty.
Last Statement

In next chapter, we will discuss auxiliary constructor.

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...