Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which situation val/var is necessary in Kotlin constructor parameter?

Tags:

android

kotlin

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)     } } 

cannot be resolved code:

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)     } } 

cannot be resolved code screenshot

The only difference is the "val" has been deleted and cannot be resolve.

Which might be important is that it's a inner class.

BUT

This one class without "val/var" in constructor parameter is working:

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'll get suggestion:

"Constructor parameter is never used as a property less. This inspection reports primary constructor parameters that can have 'val' or 'var' removed. Unnecessary usage of 'val' and 'var' in primary constructor consumes unnecessary memory."

I am just confused about it.

like image 523
Korey Lere Avatar asked Aug 22 '17 15:08

Korey Lere


People also ask

Why Val is used in Kotlin?

Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value never changes. You can't reassign a value to a variable that was declared using val .

How do you use Val in Kotlin?

The eval() function is only available in Kotlin/JS , and simply calls the JavaScript eval() function. This means you can't use it in code you might want to run on Kotlin/JVM or Kotlin/Native.


2 Answers

When you write val/var within the constructor, it declares a property inside the class. When you do not write it, it is simply a parameter passed to the primary constructor, where you can access the parameters within the init block or use it to initialize other properties. For example,

class User(val id: Long, email: String) {     val hasEmail = email.isNotBlank()    //email can be accessed here     init {         //email can be accessed here     }      fun getEmail(){         //email can't be accessed here     } } 

Constructor parameter is never used as a property

This suggestion is saying that you do not use this property in place apart from the initialization. So, it suggests you to remove this property from the class.

like image 117
BakaWaii Avatar answered Oct 27 '22 06:10

BakaWaii


Constructor parameters must use var or val when they are used as a property elsewhere in the class. They do not need to be properties if they are only used for class initialization.

In the example below, the parameter must be a property (var or val) because it is used in a method:

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

In this other example, the parameter is only used to initialize the class, so it does not need to be a property:

class B(number: Int): A(number) {     init {         System.out.println("number: $number")     } } 
like image 28
Bryan Herbst Avatar answered Oct 27 '22 08:10

Bryan Herbst