Constructor in Scala

Scala-Constructor-Auxillary

Scala constructor is used for creating an instance (object) of a class.
Scala has two types of constructor.
1. Primary constructor
2. Auxiliary constructor

In this blog, We will learn about primary constructor.

A Scala class can contain one and only one primary constructor.

Class definition

class <class-name> (parameter-list) {

}

class keyword is used to create a class, with the class definition we have to provide a parameter list of the constructor. Anything defined between the opening and closing parenthesis comes in constructor body, if you do not provide parameter list then it will behave like java default constructor.
here is an example of the default primary constructor.

You can create an object of Employee class in two ways either an empty parameter or without parameter using new keyword with the class name because Scala has a feature called syntactic sugar. When an object is created constructor automatically called.
here is an example

Now you can create a parameterized constructor like this

When we create primary constructor using the var in parameter then Scala compiler automatically create a getter and setter method for them.

First, create a class Employee.scala with contents

class Employee(var empId: Int,var name: String,var salary :Double)

Now Open Linux terminal first compile it, use scalac Employee.scala then use javap Employee.class to see generated java code.

Compiled from “Employee.scala”
public class Employee {
public int empId();  //  this is getter method
public void empId_$eq(int);  //  this is setter method
public java.lang.String name();
public void name_$eq(java.lang.String);
public double salary();
public void salary_$eq(double);
public Employee(int, java.lang.String, double);
}

Now, If you create the class using val in the primary constructor then Scala compiler generates only getter method. So You can also get value not set.

create a class Employee.scala with contents

class Employee(val empId: Int,val name: String,val salary :Double)

Now Open Linux terminal first compile it, use scalac Employee.scala then use javap Employee.class to see generated java code.

Compiled from “Employee.scala”
public class Employee {
public int empId();  //  getter method
public java.lang.String name();
public double salary();
public Employee(int, java.lang.String, double);
}

Now if you define Primary constructor using val or var with private modifier then Scala compiler will not generate getter and setter method for them.

Create a class Employee.scala with contents

class Employee(private var empId: Int,private val name :String) {
def getName = name
}

Now Open Linux terminal first compile it, use scalac Employee.scala then use javap Employee.class to see generated java code.

Compiled from “Employee.scala”
public class Employee {
public java.lang.String getName();
public Employee(int, java.lang.String);
}

Now if you do not provide var or val in Primary Constructor parameters then Scala compiler will not generate getter and setter method for them.

Create a class Employee.scala with contents

class Employee(empId: Int,name: String,salary :Double)

Now Open Linux terminal first compile it, use scalac Employee.scala then use javap Employee.class to see generated java code.

Compiled from “Employee.scala”
public class Employee {
public Employee(int, java.lang.String, double);
}

so we can not get or set value.

Learn about Auxillary Constructor in Scala

Please comment, if you have any doubt or suggestion.
thank you 🙂

knoldus-advt-sticker

1 thought on “Constructor in Scala

Leave a Reply

%d bloggers like this: