how to instantiate an object from a class in kotlin

kotlin object vs class
kotlin instantiate class
kotlin data class
kotlin class parameter
kotlin call function from another class
kotlin empty object
kotlin data class constructor
abstract class 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
}

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

Object Expressions, Object Declarations and Companion , Sometimes we need to create an object of a slight modification of some class, without explicitly declaring a new subclass for it. Kotlin handles this case with  Classes. You can create a Class in Kotlin using the class keyword - class Person { } The curly braces can be omitted if the class has no body - class Person This is the simplest class that you can have in Kotlin. You can create an object/instance of the above the class using its default constructor like so - val person = Person()

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
}

Classes and Inheritance, A class in Kotlin can have a primary constructor and one or more secondary constructors. In fact, for declaring properties and initializing them from the primary Even more specifically, if you declare a companion object inside your class,  Object Expressions and Declarations. Sometimes we need to create an object of a slight modification of some class, without explicitly declaring a new subclass for it. Kotlin handles this case with object expressions and object declarations. Object expressions. To create an object of an anonymous class that inherits from some type (or types), we write:

try to forget java

val board = Board("name")

how to instantiate an object from a class in kotlin, Kotlin does not use new . Board board = new Board(name = "ABC");. is incorrect. Use val board = Board("ABC"). Your code reflects the Java  Use a singleton (in Kotlin object) to hold the Room database instance: For instantiate abstract class in Kotlin you use object: <your class>. Example:

in kotlin

when you want to declare new object yon can do like this.

val board = Board("ABC")

if you declare object by using val keyword. it look as you use final in java. the variable which you declared can not recreate again.

var board = Board("ABC")

if you use var to declare it look as normal variable in java

Anyway in kotlin you will see something that It doesn't contain in java such as scoping function as link below. it will help you write your code is more easily.

https://kotlin.guide/scoping-functions

I hope this help :)

Kotlin Classes, Objects, Constructors and Initializers, 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 Kotlin, the topmost type in the type hierarchy is called Any. This is equivalent to the Java Object type. This means that all classes in Kotlin explicitly inherit from the Any type, including String, Int, Double, and so on. The Any type contains three methods: equals, toString, and hashcode.

Kotlin From Scratch: Classes and Objects - Tuts+ Code, But be aware that instantiation of a Kotlin class in Java will require the new keyword. 1. 2. // In a Java file. Book book =  Kotlin will allow you to declare this property without initializing it, and you can set the property value at some point after construction (either directly or via a function). It is the responsibility of the class itself as well as its users to take care not to read the property before it has been set,

Kotlin Class and Objects (With Examples), In this article, you'll be introduced to Object-oriented programming in Kotlin. You'll learn what a class is, how to create objects and use it in your program. 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.

Kotlin Object Declarations and Expressions, They are used if you need to create an object of a slight modification of some class or interface without declaring a subclass for it. For example , window. Adam, you miss the whole point: objects are singeletones by design. It's one of good scala featuras copied into kotlin. (In java or C# you have ugly static members mixed with normal class members).

Comments
  • That's not valid Kotlin
  • name = "ABC" is actually a correct way to pass the argument in Kotlin, see Functions / Named Arguments in the language reference.