Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 298 Vote(s) - 3.65 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to instantiate an object from a class in kotlin

#1
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
}
Reply

#2
try to forget java

val board = Board("name")

Reply

#3


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

[To see links please register here]


I hope this help :)


Reply

#4
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.

<s>`name = "ABC"` just isn't valid syntax no matter if it's Java or Kotlin.</s> Actually it is (from @hotkey):

[To see links please register here]

Reply

#5
**Unlike Java, in Kotlin this is the correct way**

**MainActivity.kt**

```kotlin
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**

```kotlin
class Board(val name: String) {
var age: Int = 0
}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through