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]