Mobile Development 23 min read

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.

Jike Tech Team
Jike Tech Team
Jike Tech Team
Why Kotlin Differs from Java: Constructors, init Blocks, and Static Alternatives

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.

<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

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.

<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

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.

<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

static

members with

companion object

. Inside a companion object you can declare constants with

const val

and 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

arrayOf

and support methods like

get

,

set

,

contains

. Primitive arrays have specialized types such as

IntArray

to avoid boxing.

<code>val strs: Array<String> = arrayOf("a", "b", "c")
println(strs[0])
strs[1] = "B"
</code>

Kotlin collections include immutable

listOf

,

setOf

,

mapOf

and 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

Sequence

for lazy iteration, created via

sequenceOf

,

asSequence

on an

Iterable

, or

generateSequence

with 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

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.

JavaAndroidKotlinCollectionsCompanion ObjectConstructorsVisibility Modifiers
Jike Tech Team
Written by

Jike Tech Team

Article sharing by the Jike Tech Team

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.