In what situation is val / var needed in the Kotlin constructor parameter?

Right code:

class MainActHandler(val weakActivity: WeakReference<Activity>): Handler() {
    override fun handleMessage(msg: Message?) {
        val trueAct = weakActivity.get() ?: return
        if (msg?.what == ConversationMgr.MSG_WHAT_NEW_SENTENCE){
            val sentence = msg.obj as String?
            trueAct.conversation.text = sentence
        }
        super.handleMessage(msg)
    }
}

code cannot be allowed:

class MainActHandler(weakActivity: WeakReference<Activity>): Handler() {
    override fun handleMessage(msg: Message?) {
        val trueAct = weakActivity.get() ?: return
        if (msg?.what == ConversationMgr.MSG_WHAT_NEW_SENTENCE){
            val sentence = msg.obj as String?
            trueAct.conversation.text = sentence
        }
        super.handleMessage(msg)
    }
}

code screenshot cannot be resolved

The only difference is that "val" has been removed and cannot be resolved.

What may be important is that it is an inner class.

BUT

This one class without "val / var" in the constructor parameter works:

class BookInfo(convrMgr: ConversationMgr, id: String, queue: RequestQueue, queueTag:String) {

val TAG = "BookInfo"
var title: String? = ""

init {
    val url = "https://api.douban.com/v2/book/$id"
    // Request a string response from the provided URL.
    val stringRequest = StringRequest(Request.Method.GET, url,
            Response.Listener<String> { response ->
                Log.d(TAG + " Response", response.substring(0))
                // Parse JSON from String value
                val parser = Parser()
                val jsonObj: JsonObject =
                        parser.parse(StringBuilder(response.substring(0))) as JsonObject
                // Initial book title of book properties.
                title = jsonObj.string("title")
                Log.d(TAG + " Book title", title)
                convrMgr.addNewMsg(title)
            },
            Response.ErrorListener { error -> Log.e(TAG + " Error", error.toString()) })
    // Set the tag on the request.
    stringRequest.tag = queueTag
    // Add the request to the RequestQueue.
    queue.add(stringRequest)
}

}

And if I add var / val before "queue: RequestQueue", I will get a sentence:

"The constructor parameter is never used as a property less. This inspection reports the parameters of the primary constructor, which may have" val "or" var. "The unnecessary use of" val "and" var "in the main constructor consumes unnecessary memory."

I'm just confused.

+4
2

val/var , . , , , init . ,

class User(val id: Long, email: String) {
    val hasEmail = email.isNotBlank()    //email can be access here
    init {
        //email can be access here
    }
}

, . , .

+5

var val, . , .

(var val), :

class A(val number: Int) {
    fun foo() = number
}

, :

class B(number: Int): A(number) {
    init {
        System.out.println("number: $number")
    }
}
+1

Source: https://habr.com/ru/post/1684240/


All Articles