TheBulgarianWolf

Kotlin Constructors

Apr 1st, 2021
793
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun main(args: Array<String>) {
  2.     println("person1 is instantiated")
  3.     val person1 = Person("joe", 25)
  4.  
  5.     println("person2 is instantiated")
  6.     val person2 = Person("Jack")
  7.  
  8.     println("person3 is instantiated")
  9.     val person3 = Person()
  10. }
  11.  
  12. class Person(fName: String = "Unknown", personAge: Int = 20) {
  13.     val firstName: String
  14.     var age: Int
  15.  
  16.     // initializer block
  17.     init {
  18.         firstName = fName.capitalize()
  19.         age = personAge
  20.  
  21.         println("First Name = $firstName")
  22.         println("Age = $age")
  23.     }
  24. }
RAW Paste Data