Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between properties and parameters in Kotlin?

Tags:

kotlin

Here is a simple example of a class with some code (properties) inside the bracket

class Person(firstName: String) {
....
}

Now here is an example of a function with some code (parameters) inside the bracket

fun double(x: Int) {
...
}

I know this is a fundamental question but I am quite confused as a beginner.

like image 601
J Adonai Dagdag Avatar asked Jul 11 '17 10:07

J Adonai Dagdag


People also ask

What is the difference between parameters and properties?

"Speaking generally, properties are those physical quantities which directly describe the physical attributes of the system; parameters are those combinations of the properties which suffice to determine the response of the system.

What are properties in Kotlin?

Properties in Kotlin classes can be declared either as mutable, using the var keyword, or as read-only, using the val keyword. class Address { var name: String = "Holmes, Sherlock" var street: String = "Baker" var city: String = "London" var state: String? = null var zip: String = "123456" }

What is the difference between field and property in Kotlin?

Properties vs fieldsField is just a class member variable that hold a value. It can be read-only or mutable and marked with any access modifier such as public or private . Property is more complex element that contain a private field and accessors.

What is difference between property and function?

The main purpose of the property is to allow class to expose its private variables and representing the data portion of the class, function representing the actions. In the following example, RotbotName() is a property, it representing a conception thing, a data member. VerifyName() is a function. It is an action.


2 Answers

You pass parameters to functions and constructors, and classes have properties.

The constructor of the Person class in your example has a single parameter, and so does the double function. In this case, the firstName parameter is not a property!

To make it a property, you have to declare it so:

class Person(firstName: String) {

  val firstName : String = firstName
}

Kotlin allows this to be shorter, which makes the firstName parameter serve as a property:

class Person(val firstName: String)
like image 152
nhaarman Avatar answered Sep 20 '22 06:09

nhaarman


First, your firstName also is a parameter rather than a property in Person class.

//           v-- a parameter declared on primary constructor
class Person(firstName: String)

you can access a parameter declared on primary constructor in init block or property/field declaration, for example:

class Person(firstName: String){
   val first:String
   init{ first=firstName }
}

class Person(firstName: String){
   val first:String = firstName
}

class Person(firstName: String){
   @JvmField val first:String = firstName
}

to make the firstName to a property you can using keyword val or var, for example:

//            v--- immutable property
class Person(val firstName: String)

//            v--- mutable property
class Person(var firstName: String)

a Kotlin property will generate getter/setter(?) and a backing field(?) to java byte code. Take an example of a mutable property to java byte code as below:

public final class Person{
   private String firstName; // backing field

   //                   v--- parameter
   public Person(String firstName){ this.firstName = firstName; }

   //getter
   public final String getFirstName(){ return firstName; }

   //setter
   public final String setFirstName(String firstName){ this.firstName= firstName; }
}

a parameter only visible in function scope/constructor scope except parameter declared on primary constructor.

Note: a parameter is immutable like as java effective-final variables/parameters, so you can't reassign a parameter at all in Kotlin, for example:

fun foo(bar:String){
   bar = "baz"
// ^--- compile error  
}
like image 23
holi-java Avatar answered Sep 20 '22 06:09

holi-java