syntax 1:
class Name(val param1: Type){
//statements
}
===================
In syntax 1, class declaration starts with the class keyword followed by the class name and the class header. The class header contains parameters and implicit constructor. The constructor in the class is the primary constructor.
syntax 2:
class Name public constructor(val param1: Type){
//statements
}
====================
In syntax 2, the constructor keyword was explicit since there is a modifier before it. You can omit the constructor keyword if you did not specify any modifier.
syntax 3:
class Name
=====================
In syntax 3, the class has no body. So, you can omit the braces. Usually, this is use in creating a data class where what you need is just the class header parameter and constructor
syntax 4:
class Name(){
constructor(val param2: Type): this(){
// statement
}
//statement
}
=================
In syntax 4, the class contains a secondary constructor. You need to use the constructor keyword for that. It is called secondary constructor since the first constructor is in the class header. You can create more than one secondary constructor.
===================
fun main(){
YourClass(100)
}
class YourClass(message: String = "Primary constructor"){
private val prop1: String = "Init block: "
private val prop2: String = "Secondary constructor: "
private var counter = 0
init{
counter++
println("$counter: $prop1")
}
constructor(x: Int): this(){
counter++
println("$counter: $prop2 with value $x")
}
init{
counter++
println("$counter: $prop1 containing: $message value")
}
}
===================
Result:
1: Init block:
2: Init block: containing: Primary constructor value
3: Secondary constructor: with value 100
Note:
1. init block is used for computation. You can add the parameters in class headers here. It runs before any secondary constructors. As you can see on the above example, there are two init blocks and both run before the secondary constructor.
2. The secondary constructor must delegate with the primary constructor. this() was used in the secondary constructor for delegation.
3. The properties like prop1 and prop2 above, you can assign here the parameters from class headers.
4. For example, if your class has no parameters in the class headers, the compiler will automatically create a no argument primary constructor for you. Then, if you created a secondary constructor and forgot to delegate it to the primary constructor, the compiler will implicitly delegate it for you. This is only applicable for explicit modifier and no parameter primary constructor.
5. To create an instance or object of the class, you can assign it to variable like
val object = YourClass(100)
This is equivalent to val object: YourClass = YourClass(100). Take note, in java, you use a new keyword to create an object but here in kotlin, there is no need for that.
A class can contain members. These members are the following:
a. initializer blocks
b. secondary constructors
c. properties
d. functions
e. Nested and inner classes
f. object declaration
YOU ARE READING
Kotlin Programming
RandomI have been trying to learn things online. I will put my notes in here. I put things in my own words and i do not copy their programs. I try to do things the right way so that i may learn. Below is the site where i study. Go check this out if you wa...
