Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to instantiate an object from a class in kotlin

I am learning Kotlin, and I googled how to create a class in kotlin. So, I created the below class as a test. In the main activity, I am trying to instantiate an object from the class Board, but i get the following error:

classifier Board does not have a companion object

please let me know how to intantiate an object of an the class Board?

MainActivity:

class ActMain : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.layout_act_main)

    Board board = new Board(name = "ABC");
}
}

Board.kt:

data class Board(val name: String) {
    var age: Int = 0
}
like image 835
LetsamrIt Avatar asked Sep 09 '18 14:09

LetsamrIt


People also ask

How do I create an instance of a class in Kotlin?

In Kotlin, we cannot create an instance of an abstract class. Abstract classes can only be implemented by another class which should be abstract in nature. In order to use an abstract class, we need to create another class and inherit the abstract class.

What does it mean to instantiate an object from a class?

Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor.

Can we use the new keyword to instantiate a class object in Kotlin?

Notice that, unlike other object-oriented programming languages like Java, You don't need to use the new keyword to instantiate a class in Kotlin. In fact, new is not a keyword in Kotlin.

How to create objects in Kotlin?

Before you create objects in Kotlin, you need to define a class. A class is a blueprint for the object. We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.

What is a class in Kotlin?

Kotlin Class. Before you create objects in Kotlin, you need to define a class. A class is a blueprint for the object. We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.

What is the difference between abstract and open in Kotlin?

Learn more about inheritance in Kotlin. A class may be declared abstract, along with some or all of its members. An abstract member does not have an implementation in its class. You don't need to annotate abstract classes or functions with open. You can override a non-abstract open member with an abstract one.

Why should I learn Kotlin?

You'll learn what a class is, how to create objects and use it in your program. Kotlin supports both functional and object-oriented programming. Kotlin supports features such as higher-order functions, function types and lambdas which makes it a great choice for working in functional programming style.


3 Answers

Kotlin does not use new.

Board board = new Board(name = "ABC");

is incorrect. Use

val board = Board("ABC")

Your code reflects the Java syntax... sort of. Kotlin has type inference, so you don't need to specify the class type. However, if you do specify it, it's different from Java:

val board: Board = Board("ABC")

Semi-colons are also not generally used in Kotlin, although they won't break the compilation if you use them.

name = "ABC" just isn't valid syntax no matter if it's Java or Kotlin. Actually it is (from @hotkey): https://kotlinlang.org/docs/reference/functions.html#named-arguments

like image 117
TheWanderer Avatar answered Sep 21 '22 15:09

TheWanderer


try to forget java

val board = Board("name")
like image 38
tamtom Avatar answered Sep 23 '22 15:09

tamtom


Unlike Java, in Kotlin this is the correct way

MainActivity.kt

class ActMain : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_act_main)

        val board = Board("ABC")
        board.age = 12
    }
}

Board.kt

class Board(val name: String) {
    var age: Int = 0
}
like image 36
Aarón Lucker Avatar answered Sep 20 '22 15:09

Aarón Lucker