DEV Community

Felipe Cezar
Felipe Cezar

Posted on

From Java to Kotlin: How Would You Rewrite This Class?

Let's play a little game. You're a developer who just received a task:

"Take this Java class and rewrite it in Kotlin β€” keeping the exact same behavior."

Here's the Java class you've been handed:

public class AlunoJava {
    private String codigo;
    private String nome;
    private int numero = 0;
    private String texto = "EscolaX";

    public AlunoJava(String codigo, String nome) {
        this.codigo = codigo;
        this.nome = nome;
    }
}
Enter fullscreen mode Exit fullscreen mode

Take a moment. How would you write this in Kotlin? πŸ€”


Understanding What This Class Does

Before jumping to Kotlin, let's break down what this Java class actually contains:

  • Two private fields (codigo and nome) that are set via the constructor
  • Two private fields with default values (numero = 0 and texto = "EscolaX")
  • A constructor that receives codigo and nome as parameters and assigns them using this

Simple enough. Now let's talk about how Kotlin handles each of these.


Step 1: The Class Declaration

In Java, you write:

public class AlunoJava { ... }
Enter fullscreen mode Exit fullscreen mode

In Kotlin, classes are public by default, so you don't need the public keyword:

class AlunoKotlin { ... }
Enter fullscreen mode Exit fullscreen mode

βœ… No public needed β€” less boilerplate from the start.


Step 2: The Constructor

This is where Kotlin really shines. In Java, the constructor is a separate block:

public AlunoJava(String codigo, String nome) {
    this.codigo = codigo;
    this.nome = nome;
}
Enter fullscreen mode Exit fullscreen mode

In Kotlin, you can declare the primary constructor directly in the class header. And if you add val or var to the parameters, Kotlin automatically creates the fields and assigns them β€” no need for this.x = x at all:

class AlunoKotlin(private val nome: String, private val codigo: String) {
    // nome and codigo are already declared AND assigned here!
}
Enter fullscreen mode Exit fullscreen mode

βœ… The constructor declaration, field declaration, and assignment all happen in one line.


Step 3: val vs var

You might be wondering: why val instead of var?

  • val β†’ immutable (like final in Java) β€” value can't be reassigned after initialization
  • var β†’ mutable β€” value can be changed later

Since the original Java class doesn't reassign codigo or nome after construction, val is the more faithful and idiomatic translation.


Step 4: Fields With Default Values

In Java:

private int numero = 0;
private String texto = "EscolaX";
Enter fullscreen mode Exit fullscreen mode

In Kotlin, these go inside the class body, and the types are inferred automatically:

private var numero = 0          // Kotlin infers Int
private var texto = "EscolaX"   // Kotlin infers String
Enter fullscreen mode Exit fullscreen mode

βœ… We use var here because these fields may be changed later (they weren't final in Java).


The Final Result

Putting it all together:

class AlunoKotlin(private val nome: String, private val codigo: String) {
    private var numero = 0
    private var texto = "EscolaX"
}
Enter fullscreen mode Exit fullscreen mode

Compare side by side:

Java (17 lines):

public class AlunoJava {
    private String codigo;
    private String nome;
    private int numero = 0;
    private String texto = "EscolaX";

    public AlunoJava(String codigo, String nome) {
        this.codigo = codigo;
        this.nome = nome;
    }
}
Enter fullscreen mode Exit fullscreen mode

Kotlin (3 lines):

class AlunoKotlin(private val nome: String, private val codigo: String) {
    private var numero = 0
    private var texto = "EscolaX"
}
Enter fullscreen mode Exit fullscreen mode

Same behavior. A fraction of the code. 🎯


Key Takeaways

Java concept Kotlin equivalent
public class class (public by default)
Separate constructor block Primary constructor in class header
this.x = x assignment val/var in constructor parameters
private String x private val x: String
Type before name (String x) Type after name (x: String)
final field val
Mutable field var

Conclusion

Kotlin was designed to eliminate Java boilerplate without sacrificing clarity. Once you get used to the primary constructor syntax and the val/var distinction, you'll find that most simple Java classes translate to Kotlin naturally β€” and end up much shorter.

Have you started using Kotlin in any of your projects? Drop a comment below! πŸ‘‡

Top comments (0)