Declare a variable without an initial value

前端 未结 1 811
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 02:47

This Scala tutorial has the following to say about declaring variables without an initial value:

If you do not assign any initial value to a variable,

相关标签:
1条回答
  • 2020-12-30 03:08

    The error is correct, you can only do that on an abstract class or trait. The tutorial might be assuming that you are writing that code inside of an abstract class.

    It is possible to initialize variables to some default value:

    var i: Int = _
    var s: String = _
    

    But that's essentially the same as:

    var i: Int = 0
    var s: String = null
    
    0 讨论(0)
提交回复
热议问题