N Sharma
N Sharma

Reputation: 34487

Property getter or setter expected in Kotlin

I am learning Kotlin from official docs, I am trying to create a class to do arithmetic operations.

class Sum {

    var a: Int, b: Int;

    constructor(a: Int, b: Int) {
        this.a = a
        this.b = b
    }

    fun add(): Int {
        return a + b
    }
}

I have this class, now I want to create an object of this class like

val sum = Sum(1,2)
Log.d("Sum", sum.add())

I am getting this error in Sum class:

Property getter or setter expected

on b: int; within the line var a: Int, b: Int;

Upvotes: 10

Views: 40543

Answers (6)

blackHawk
blackHawk

Reputation: 6307

This error occurs because

  • Kotlin dont have support if you declare and even initialize 2 field variables on same line

  • There is default setter and getter associated with each field variable in kotlin

so when you declared them on same line, the first variable initialized with default setters and getters but the other one expects that you will give your own setter and getter because syntax was wrong

Error can be resolved via 2 ways

  • Declare setter and getter separately like this

    var a:Int
      get() {
         return 0
      }
      set(value) {
          val b = value
      } 
    
  • Correct the syntax

     var a: Int
     var b: Int
    

Upvotes: 0

Abhishek Luthra
Abhishek Luthra

Reputation: 2665

Firstly , you must put property declarations on separate lines . You can refer this thread here , where JetBrains' Engineer yole states that:

Declaring multiple properties on the same line is frowned upon by many Java style guides, so we did not implement support for that in Kotlin.

Once property declaration on separate lines is done , you should have no problem as kotlin genrates default getter and setter internally with same visibility as your property . If you do not specify any visibility modifier, public is used by default, which means that your declarations will be visible everywhere .

In fact, Kotlin has a concise syntax, for declaring properties and initializing them from the primary constructor(In case you have only one constructor which will be always primary):

  class Sum(var a:Int,var b:Int) {

    fun add(): Int {
        return a + b
    }
}

Refer constructors column in official documentation in link below https://kotlinlang.org/docs/reference/classes.html

Upvotes: 3

GhostCat
GhostCat

Reputation: 140417

var a: Int, b: Int;

Kotlin doesn't allow to have multiple declarations in one line. You have to go for:

var a: Int
var b: Int

instead. The Kotlin folks simply found the C/java practice of "int a, b, .." to be something they wish to not support in Kotlin.

Upvotes: 15

Mario
Mario

Reputation: 1781

The error you have would be solved simply declaring the variables in two lines:

var a: Int
var b: Int

However, the recommended way to integrate those variables in constructors (if you only want to have a single constructor with arguments):

class Sum(var a: Int, var b: Int) {
    fun add(): Int = a + b
}

Upvotes: 6

Sachin Chandil
Sachin Chandil

Reputation: 17809

You are writing unnecessary code in your class.

Write short form for constructor if there is only one.

If there are properties in class, they can be defined in constructor by using val or var.

Use it like following:

class Sum (var a: Int,var b: Int){

    fun add(): Int {
        return a + b
    }
}

Do read Basic Syntax of Kotlin.

Upvotes: 9

Angel Koh
Angel Koh

Reputation: 13485

the following code shows you how to assign and manipulate variables in your class

class Sum (var a: Int,var b: Int){

    fun add(): Int= a + b //you can return the results directly.
}

you can test your code using the main below.

fun main(args: Array<String>) {
   var s= Sum(1,2)

    print(s.add())
}

Upvotes: 1

Related Questions