Why Kotlin Differs from Java: Constructors, init Blocks, and Static Alternatives
This article explores the key differences between Kotlin and Java for Android development, covering constructors, init blocks, val versus final, static equivalents with companion objects, top‑level declarations, arrays, collections, sequences, visibility modifiers, and includes practical exercises.
Constructor
In Kotlin constructors are declared with the constructor keyword instead of sharing the class name as in Java, and they are public by default, so the public modifier is omitted.
public class User {
int id;
String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
} class User {
val id: Int
val name: String
constructor(id: Int, name: String) {
this.id = id
this.name = name
}
}The differences are the explicit constructor keyword and the absence of the public modifier.
init
Kotlin uses an init block for initialization code that runs before the constructor, similar to Java's instance initializer.
public class User {
{
// init block runs before constructor
}
public User() {}
} class User {
init {
// init block runs before constructor
}
constructor() {}
}final
In Kotlin the val keyword represents a read‑only variable, analogous to Java's final. Function parameters are val by default, so they do not need an explicit modifier.
final int final1 = 1;
void method(final String final2) {}
final String final3 = "The parameter is " + final2; val fina1 = 1
fun method(final2: String) {
val final3 = "The parameter is " + final2
}static property / function
Kotlin replaces Java's static members with companion object. Inside a companion object you can declare constants with const val and regular static‑like members.
public static final String CONST_STRING = "A String"; class Sample {
companion object {
const val CONST_NUMBER = 1
}
}You can also create a nested object to hold static members:
class A {
object B {
var c: Int = 0
}
}
// Access: A.B.ctop‑level declaration
Kotlin allows functions and properties to be declared outside of any class. These top‑level members belong to the package and can be imported directly.
package com.hencoder.plus
fun topLevelFunction() {}Arrays and Collections
Kotlin arrays are generic classes created with arrayOf and support methods like get, set, contains. Primitive arrays have specialized types such as IntArray to avoid boxing.
val strs: Array<String> = arrayOf("a", "b", "c")
println(strs[0])
strs[1] = "B"Kotlin collections include immutable listOf, setOf, mapOf and mutable counterparts mutableListOf, mutableSetOf, mutableMapOf. Maps support bracket syntax for getting and setting values.
val map = mutableMapOf("key1" to 1, "key2" to 2)
val value1 = map.get("key1")
val value2 = map["key2"]
map["key1"] = 2Sequence
Kotlin introduces Sequence for lazy iteration, created via sequenceOf, asSequence on an Iterable, or generateSequence with a lambda.
val seq = sequenceOf("a", "b", "c")
val listSeq = listOf("a", "b", "c").asSequence()
val numbers = generateSequence(0) { it + 1 }Visibility Modifiers
Kotlin provides four visibility modifiers: public – default, visible everywhere (explicit public is optional). private – visible inside the class or file. protected – private plus visible to subclasses. internal – visible within the same module, replacing Java's package‑private visibility.
Unlike Java, Kotlin does not have a package‑private modifier; internal serves a similar purpose at the module level. The protected modifier in Kotlin is narrower than Java's because there is no package visibility.
Exercises
Create a Kotlin class that cannot be instantiated via its constructor from outside and provides at least one alternative instantiation method.
Implement the task of storing numbers 1‑100,000 and computing their average using Array, IntArray, and List, then print the execution time for each.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
