Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Scala Online

class MyClass ( val name : String,  val rollno : Int) /* Primary Constructor */
{   //When a class is instantiated everything inside the class get executed except the methods. Its unique features of scala unlike java and c++.
    println("Print everytime the class is instantiated")
    //Auxillary Constructor
    def this ( name : String){ //First Line has to be primary constructor
        this(name,100)
        println("Hello Auxillary Constructor")
       //this(name,100)
    }
    def test() : Unit = {
        println("Test Mesaage "+name  +rollno)
    }
}

object HelloWorld {
   def main(args: Array[String]) {
       val m = new MyClass("kedar",1)  //m is an instance
       m.test()
       println(m.name)
       println(m.rollno)
       
       val n = new MyClass("Sugyani")  //n is an instance
       n.test()
   }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.