harshil9968
harshil9968

Reputation: 3244

String passed to Class appear null

Here is my short Code :

class InnoString(abc: String)extends App{

  val message=abc

  override def toString(): String ={
    return message
  }



  def concat(sub : String): String={
      return  s"$sub$message"
    }
}



object checkInno extends App{
  val x = new InnoString("harshil")
  println(x)
  println(x.concat("there "))
}

The result now is :

here
null
there null

Expected output is :

here
harshil
there harshil

Upvotes: 0

Views: 38

Answers (1)

pedrofurla
pedrofurla

Reputation: 12783

This happens because App changes how variables are initialized. Remove the extends App and it will behave as you expect.

Upvotes: 1

Related Questions