# Difference between property and local variable

**URL:** <https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155>\
**Category:** Language Design\
**Created:** [August 29, 2024, 7:49am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155 "2024-08-29T07:49:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![andaupro](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andaupro/32/10939_2.png) [@andaupro](https://discuss.kotlinlang.org/u/andaupro)\
**Post date:** [August 29, 2024, 7:49am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/1 "2024-08-29T07:49:39Z")

</div>

Hi everyone,

I’m newbie in Kotlin. While reading [Properties | Kotlin Documentation](https://kotlinlang.org/docs/properties.html#checking-whether-a-lateinit-var-is-initialized), I came across the following:

“This modifier can be used on `var` properties declared inside the body of a class (not in the primary constructor, and only when the property does not have a custom getter or setter), as well as for top-level properties and local variables.”

it means lateinit modifier can be used inside the body of a class as well as for top-level properties and local variables.

As I know, in kotlin we have a terminology “Property” that consists of backing field (the only “field” terminology I know)

So what is “local variable” in kotlin? is it different with property?

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [August 29, 2024, 8:19am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/2 "2024-08-29T08:19:24Z")

</div>

> [@andaupro](#):
>
> So what is “local variable” in kotlin? is it different with property?

> **[Basic syntax | Kotlin](https://kotlinlang.org/docs/basic-syntax.html#variables)**

A local variable is a variable within a visibility scope, so it can be accessed locally only.  
In the [same page](https://kotlinlang.org/docs/basic-syntax.html#creating-classes-and-instances), `perimeter` is a property, `rectangle` is a local variable:

```kotlin
class Rectangle(val height: Double, val length: Double) {
    val perimeter = (height + length) * 2 
}
fun main() {
    val rectangle = Rectangle(5.0, 2.0)
    println("The perimeter is ${rectangle.perimeter}")
}

```

---

<div class="post-metadata">

**Author:** ![andaupro](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andaupro/32/10939_2.png) [@andaupro](https://discuss.kotlinlang.org/u/andaupro)\
**Post date:** [August 29, 2024, 8:53am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/3 "2024-08-29T08:53:51Z")

</div>

so rectangle can be called as local property? what diff between local property and local variable? after reading the guide, I think it seem to be the same but if it’s the same, why the guide said like “for top-level properties and local variables”?

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [August 29, 2024, 9:24am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/4 "2024-08-29T09:24:47Z")

</div>

Properties are something that exist on an object for as long as that object exists. Local variables can exist inside functions or in a constructor. Local variables inside a function are only visible inside that function, and disappear when the function ends. Local variables in a constructor are visible to any initialization code inside the class body (initializing properties or `init` blocks), and disappear when the class finishes being created.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [August 29, 2024, 9:25am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/5 "2024-08-29T09:25:00Z")

</div>

> [@andaupro](#):
>
> so rectangle can be called as local property?

No.

---

<div class="post-metadata">

**Author:** ![andaupro](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andaupro/32/10939_2.png) [@andaupro](https://discuss.kotlinlang.org/u/andaupro)\
**Post date:** [August 29, 2024, 9:37am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/6 "2024-08-29T09:37:52Z")

</div>

got it! I’ve just started learning Kotlin and I’m struggling to wrap my head around all the Kotlin jargon, especially this stuff. Thanks a lot all^^

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [August 29, 2024, 8:18pm UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/7 "2024-08-29T20:18:05Z")

</div>

Please note this is not really a Kotlin jargon, it is rather an industry standard. Most languages have [local variables](https://en.wikipedia.org/wiki/Local_variable) and either [properties](https://en.wikipedia.org/wiki/Property_(programming)), [fields](https://en.wikipedia.org/wiki/Field_(computer_science)) or both. Of course, there are some differences between languages, for example in Kotlin constructor params (so local vars) are not scoped to a function, but to a class body (let’s simplify). But generally, these concepts are rather common.

---

<div class="post-metadata">

**Author:** ![Junior](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Junior](https://discuss.kotlinlang.org/u/Junior)\
**Post date:** [December 5, 2024, 12:15am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/8 "2024-12-05T00:15:57Z")

</div>

Incorrect, the members defined in the constructor are properties, as described in the documentation.

Reference: [Kotlin documentation](https://kotlinlang.org/docs/basic-syntax.html#creating-classes-and-instances)

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [December 5, 2024, 12:30am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/9 "2024-12-05T00:30:11Z")

</div>

That’s only if you use the `val` or `var` keyword. For instance:

```auto
class Foo(x: Int) {
  val y = x // compiles fine
  fun doSomething() {
    println(x) // doesn't compile
  }
}

```

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [December 5, 2024, 2:41am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/10 "2024-12-05T02:41:13Z")

</div>

I guess the docs aren’t super clear on the difference, but it definitely works how I said it does. 😛

---

<div class="post-metadata">

**Author:** ![Junior](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Junior](https://discuss.kotlinlang.org/u/Junior)\
**Post date:** [December 6, 2024, 1:02am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/11 "2024-12-06T01:02:38Z")

</div>

Yep, you is correct. I made this grotesque mistake, it is only a parameter when `var` or `val` is defined. In other cases it will only be parameters, in others words it will be a variable.

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [December 12, 2024, 9:54am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/12 "2024-12-12T09:54:19Z")

</div>

Not being pedantic for no reason, but I want to make sure you understand the difference in the words you’re using.

When you say “in others words it will be a variable”, that’s technically true but also inaccurate. `val` and `var` are used to declare variables as well. Constructor parameters, class properties, and function parameters are all variables. So in Kotlin, a constructor can have properties, when you use `val` or `var`, and it can have parameters. But both constructor properties _and_ constructor parameters are variables.

---

<div class="post-metadata">

**Author:** ![Junior](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Junior](https://discuss.kotlinlang.org/u/Junior)\
**Post date:** [December 13, 2024, 2:45am UTC](https://discuss.kotlinlang.org/t/difference-between-property-and-local-variable/29155/14 "2024-12-13T02:45:44Z")

</div>

Correct.  
It is not wrong to use the term _“variable”_ to refer to _“properties”_ in Kotlin, but it is not entirely correct to strictly differentiate between “properties” and “variables.” I chose the term “variable” because it encompasses both a “variable,” as it is commonly known, and a “property.” After all, a property is a variable, but not every variable is a property.

This reasoning follows the logic of a Venn diagram, for example.

In this case, I was brief and didn’t go into much detail. If we were to quantify it, and if I were correct, it would be at most about 40% when using the term “variable” in a general way, as I did.

It’s a shame to see that documentation also uses the term “variable” for properties just because it’s easier or more generic. That won’t do; if you’re going to teach something to someone, you can’t tell them that 2 = 3.
