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:
  • 232 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is difference between object and data class in Kotlin?

#1
What is difference between data and object class in Kotlin, and what is the purpose of each?


data class User(val name: String, val age: Int)

and

object user {
val name = ""
fun printName(name: String) = "Hello, $name!"
}
Reply

#2
Kotlin's `object` is similar to a class in Java, where all methods and variables are static.

```
object User {
val name = ""
fun printName(name: String) = "Hello, $name!"
}
```

in Kotlin is similar to the following in Java:

```
class User {
public static String name = "";
public static String printName(name: String) {
return "Hello " + name + "!";
}
}
```

Usage example:

//Kotlin
User.printName(User.name)

//Java
User.printName(User.name);

An `object` isn't exactly the same as the Java comparison I gave, though. It can inherit interfaces and classes, and the object itself is instantiated as a singleton instance. If you annotate methods inside an object with `@JvmStatic`, they will become true static members.

[Kotlin's object](

[To see links please register here]

)

___

The `data` class in Kotlin is just a simpler syntax for a class that has no (or minimal) logic, and contains certain values. Kotlin generates the `equals()`, `hashCode()` and `toString()` functions for you in a data class, along with some other helper functions.

```
data class User(val name: String, val age: String)
```

in Kotlin will look something like this in Java:

```
class User {
public final String name;
public final String age;

public User(String name, String age) {
this.name = name;
this.age = age;
}

@Override
public boolean equals(Object other) {
//Kotlin-generated equality check
}

@Override
public long hashCode() {
//Kotlin's hashcode
}

@Override
public String toString() {
return "User(name=" + name + ",age=" + age + ")";
}

//other generated methods
}
```

[Kotlin's data class documentation](

[To see links please register here]

)
Reply

#3
In short, object is used, if you want to create singleton, unique object for the class and data class is a class that has `equals`, `hashCode`, `toString` automatically generated.
Reply

#4
First, there is no _object class_, the feature you are referring to is called **object declaration**.

### [Object declaration][1]

This is a feature in Kotlin that allows you implement a [singleton][2]. The object declaration combines a class declaration and a declaration of a single instance of the class in a single statement.

```
// Let's assume that class Person is defined somewhere else
object Payroll {
val employees = arrayListOf<Person>()

fun calculateSalary() {
for (person in employees) {
// ...
}
}
}

// calling methods and properties
>>> Payroll.employees.add(Person("John", 23)) // calling a property
>>> Payroll.calculateSalary() // calling a method
```

Just like a class, an object declaration can contain declarations of *properties, methods, initializer blocks,* and so on. The only thing they are not allowed are **constructors** (either primary or secondary).

Object declarations are created immediately at the point of the definition, not through constructor calls from other places in the code.

**Note:** the `object` keyword can also be used for [_companion objects_][3] and [_object expressions_][4].

### [Data Class][5]

It is very common to create classes whose main goal is to hold data. If you want your class to be a convenient holder for your data you need to override the universal object methods:

* `toString()` - string representation
* `equals()` - object equality
* `hashCode()` - hash containers

However, by adding the modifier data to your class, the necessary methods are automatically added for you. In addition, the following methods are also generated:

* `componentN()` functions corresponding to the properties in their order of declaration
* `copy()` function

```
class PersonClass(val name: String, val age: Int) // regular class
data class PersonDataClass(val name: String, val age: Int) // data class
```

In summary, if you need a holder for data, you should use a data class which means adding the modifier data to your class. This will generate the following methods for you: `toString()`, `equals()`, `hashCode()`, `componentN()`, and `copy()`, so you avoid writing boilerplate code. On the other hand, if you need to create a singleton, you use the object declaration feature.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

Reply

#5
`object`
---

`object` is Kotlin's way to create a singleton (one instance class) which is instantiated by the compiler.

---
`data class`
---

A data class is like a usual class but with a few advantages/resctrictions ([Source][1]).

**Advantages**

- `equals()`/`hashCode()`
- `toString()`
- `componentN()`
- `copy()`

Those are created from the properties specified in the primary constructor.

**Restrictions**

- The primary constructor needs to have at least one parameter;
- All primary constructor parameters need to be marked as val or var;
- cannot be abstract, open, sealed or inner;
- (before 1.1) may only implement interfaces.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

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