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
constructorkeyword instead of sharing the class name as in Java, and they are public by default, so the
publicmodifier is omitted.
<code>public class User {
int id;
String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
}</code> <code>class User {
val id: Int
val name: String
constructor(id: Int, name: String) {
this.id = id
this.name = name
}
}</code>The differences are the explicit
constructorkeyword and the absence of the
publicmodifier.
init
Kotlin uses an
initblock for initialization code that runs before the constructor, similar to Java's instance initializer.
<code>public class User {
{
// init block runs before constructor
}
public User() {}
}</code> <code>class User {
init {
// init block runs before constructor
}
constructor() {}
}</code>final
In Kotlin the
valkeyword represents a read‑only variable, analogous to Java's
final. Function parameters are
valby default, so they do not need an explicit modifier.
<code>final int final1 = 1;
void method(final String final2) {}
final String final3 = "The parameter is " + final2;</code> <code>val fina1 = 1
fun method(final2: String) {
val final3 = "The parameter is " + final2
}</code>static property / function
Kotlin replaces Java's
staticmembers with
companion object. Inside a companion object you can declare constants with
const valand regular static‑like members.
<code>public static final String CONST_STRING = "A String";</code> <code>class Sample {
companion object {
const val CONST_NUMBER = 1
}
}</code>You can also create a nested object to hold static members:
<code>class A {
object B {
var c: Int = 0
}
}
// Access: A.B.c</code>top‑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.
<code>package com.hencoder.plus
fun topLevelFunction() {}</code>Arrays and Collections
Kotlin arrays are generic classes created with
arrayOfand support methods like
get,
set,
contains. Primitive arrays have specialized types such as
IntArrayto avoid boxing.
<code>val strs: Array<String> = arrayOf("a", "b", "c")
println(strs[0])
strs[1] = "B"
</code>Kotlin collections include immutable
listOf,
setOf,
mapOfand mutable counterparts
mutableListOf,
mutableSetOf,
mutableMapOf. Maps support bracket syntax for getting and setting values.
<code>val map = mutableMapOf("key1" to 1, "key2" to 2)
val value1 = map.get("key1")
val value2 = map["key2"]
map["key1"] = 2
</code>Sequence
Kotlin introduces
Sequencefor lazy iteration, created via
sequenceOf,
asSequenceon an
Iterable, or
generateSequencewith a lambda.
<code>val seq = sequenceOf("a", "b", "c")
val listSeq = listOf("a", "b", "c").asSequence()
val numbers = generateSequence(0) { it + 1 }
</code>Visibility Modifiers
Kotlin provides four visibility modifiers:
public– default, visible everywhere (explicit
publicis optional).
private– visible inside the class or file.
protected–
privateplus 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;
internalserves a similar purpose at the module level. The
protectedmodifier 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.
Jike Tech Team
Article sharing by the Jike Tech Team
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.