JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Primary and Auxiliary Constructors in Scala

June 29, 2012 by Mohamed Sanaulla Leave a Comment

Introduction to Scala Constructor

Constructors in Scala are a bit different than in Java. Scala has 2 types of constructors:

  • Primary Constructor
  • Auxiliary Constructor

also read:

  • Primary and Auxiliary Constructors in Scala
  • Traits in Scala
  • Java HelloWorld vs Scala HelloWorld

Primary Constructor

In Java we have a no-args default constructor which is provided for every class which doesn’t provide its own constructor methods. On a similar lines Primary Constructor in Scala is the kind-of default constructor in the way every class in Scala would have a Primary Constructor.

In Scala constructors, the primary constructor spans the complete class definition i.e in the example below the age field gets initialized as part of the Primary Constructor of the Employee class.

[code lang=”java”]
class Employee{
var age:Int = 20
}
[/code]

In the above example the primary constructor didn’t accept any parameters. But the primary constructor can also accepts parameters, this is where it is different from default constructors in Java. Let me not try to draw an analogy any further.

[code lang=”java”]
class Employee(val firstName:String,
val lastName:String){

override def toString():String = {

"First Name: "+firstName+" Last Name: "+lastName
}
}
[/code]

Auxiliary Constructor

In Java one can overload the constructors to provide different constructors accepting different parameters. On similar lines Scala classes can declare Auxiliary constructors which are overloaded forms of the Primary Constructor. The auxiliary constructors are named as this.
Lets see an example:

[code lang=”java”]
class Employee(val firstName:String, val lastName:String){
var age:Int = 0

//Auxiliary Constructor
def this(firstName:String, lastName: String, age:Int){

this(firstName,lastName)
this.age = age

}

override def toString():String = {
"First Name: "+firstName+" Last Name: "+lastName
}

}
[/code]

There’s a catch here- The auxiliary constructor can invoke the primary constructor or an auxiliary constructor declared just before it, which means the below code will not work:

[code lang=”java”]
class Employee(val firstName:String, val lastName:String){
var age:Int = 0
var city:String = _

def this(firstName:String, lastName: String,
city:String, age:Int){
this(firstName, lastName, city)
this.age = age

}
def this(firstName:String, lastName: String, city:String){
this(firstName,lastName)
this.city = city

}

override def toString():String = {
"First Name: "+firstName+" Last Name: "+lastName
}

}
[/code]

Trying to compile the above code results in:

[shell]
$ scalac Employee.scala
Employee.scala:9:error: called constructor’s definition must
precede calling constructor’s definition
this(firstName, lastName, city)
^
one error found
[/shell]

The error clearly says what’s wrong. One would have to take care of this, though the compiler would report such issues, but its always good to know.

Filed Under: Scala Tagged With: scala

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved