• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • 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)
  • 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)

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.

class Employee{
  var age:Int = 20
}

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.

class Employee(val firstName:String,
               val lastName:String){

  override def toString():String = {

    "First Name: "+firstName+" Last Name: "+lastName
  }
}

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:

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
  }

}

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:

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
  }

}

Trying to compile the above code results in:

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

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.

Category: ScalaTag: 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.

Previous Post: « Package Objects in Scala
Next Post: Book Review: Java Concurrency in Practice »

Reader Interactions

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.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact