Scala constructor and method

Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-08 12:51:29 Viewed : 238


Scala constructor and method 

Just like other OOP languages, Scala classes have methods, and this is what the Scala method syntax looks like:

·        Define a  Scala class function(method ) 

def methodName(parameters : typeofparameters) : returntypeoffunction = { 

// statements to be executed  

}   

     object. methodName () //calling method name

·        Define a  Scala class constructor  

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

    def printFullName() = println(s"$firstName $lastName")

} 

Just like other OOP languages, Scala classes have methods, and this is what the Scala method syntax looks like: 

    def sum(a: Int, b: Int): Int = a + b 

    def concatenate(s1: String, s2: String): String = s1 + s2

You don’t have to declare a method’s return type, so it’s perfectly legal to write those two methods like this, if you prefer:

     def sum(a: Int, b: Int) = a + b

    def concatenate(s1: String, s2: String) = s1 + s2

This is how you call those methods:

    val x = sum(1, 2)

    val y = concatenate("foo", "bar")

 Example:

Following example illustrates about Scala constructor and method

Save the file as −  ConstructorTest.scala. 

 ConstructorTest.scala

package runnerdev 

class EmployeeTest(id:Int, name:String){     // Primary constructor 

    def display(){ 

        println(id+" "+name

    } 

object ConstructorTest

    def main(args:Array[String]){ 

        var emp = new EmployeeTest(111,"Ram")   // Passing values to constructor 

        emp.display()                // Calling a function by using an object 

    } 

compile and run the above example as follows 

scala> scalac ConstructorTest.scala

scala> scala ConstructorTest 

Output:

111 Ram


  

Example:

Following example illustrates about simple Scala functions

Save the file as −  MethodCall.scala.

 

MethodCall.scala. 

package runnerdev 

object MethodCall {

  def main(args: Array[String]) { 

    var result = functionExample() // Calling function

    println(result)

  } 

  def functionExample() = { // Defining a function

    var a = 10

    a //Its a return value

  }

 

compile and run the above example as follows 

scala> scalac MethodCall.scala

scala> scala MethodCall 

Output:

10

 

Example:

Following example illustrates about Scala functions with params

Save the file as −  MethodCall.scala. 

MethodCall.scala

package runnerdev 

object MethodCall {

  def main(args: Array[String]) { 

    var result = functionExample() // Calling function

    println("result " + result)

    var a = 10

    var b = 20 

    var addResult = functionExample(a, b) // Calling function

    println("addResult " + addResult)

  } 

  def functionExample() = { // Defining a function

    var a = 10

    a // or  retunr a .Its a return value

  } 

  def functionExample(a: Int = 0, b: Int = 0): Int = { // Parameters with default values as 0

    a + b

  }

compile and run the above example as follows 

scala> scalac MethodCall.scala

scala> scala MethodCall

 Output:

Output:

result 10

addResult 30

Search
Sub-Categories
Related Articles

Leave a Comment: