Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

338
Vistas
Kotlin lateinit correspondent java

Hello when I use Kotlin to program Android I have seen lateinit in the code. What is the equivalent in java? How can I change this code from Kotlin to Java?

public class MyTest {
    lateinit var subject: TestSubject
}
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

lateinit in Kotlin is there so that you can have non-nullable types on variables that you can't initialize at the moment the class containing them is created.

Using your example, if you didn't use lateinit, you'd have to make the subject nullable, since it has to be initialized with a value.

public class MyTest {
    var subject: TestSubject? = null
}

This would force you to do null checks every time you use it, which is ugly, so you can mark it as lateinit instead.


In Java, you don't really have this problem, since everything is nullable, and declaring an uninitialized field is nothing special:

public class JavaTest {
    TestSubject subject;
}

This initializes subject to null, so it's practically equivalent to the non-lateinit Kotlin example.

The only real difference between the lateinit version in Kotlin and the Java version is that you get a more specific exception when trying to access an uninitialized property in Kotlin, namely, a UninitializedPropertyAccessException, which will make debugging it easier than having to look for the cause of a generic NullPointerException.


If you really wanted this slightly different behavior, you could wrap your Java properties in some sort of wrappers, but I don't think it would be worth the syntactic overhead to do so. A very basic (not thread safe, for example) way to do this would be:

Have a generic wrapper class for properties:

public class Property<T> {

    private T value = null;

    public T get() {
        if (value == null)
            throw new UninitializedPropertyAccessException("Property has not been initialized");
        return value;
    }

    public void set(T value) {
        if (value == null)
            throw new IllegalArgumentException("Value can't be null");
        this.value = value;
    }

}

Use this wrapper in your classes:

public class JavaTest {
    Property<TestSubject> subject = new Property<>();
}

And then this usage would give you the uninitialized exception:

JavaTest test = new JavaTest();
test.subject.get();

And this one would run fine:

JavaTest test = new JavaTest();
test.subject.set(new TestSubject());
test.subject.get();

Edit: this is very similar to how lateinit works in Kotlin, if you decompile the bytecode of your example to Java, this is what you get:

public final class MyTest {
   @NotNull
   public TestSubject subject;

   @NotNull
   public final TestSubject getSubject() {
      TestSubject var10000 = this.subject;
      if(this.subject == null) {
         Intrinsics.throwUninitializedPropertyAccessException("subject");
      }
      return var10000;
   }

   public final void setSubject(@NotNull TestSubject var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.subject = var1;
   }
}

Basically, the compiler puts the code for checking the property access inside the class itself (+ uses some helper methods) instead of using a wrapper, which is more efficient.

about 4 years ago · Santiago Trujillo Denunciar

0

There is no such property in Java which delays the initialization of property. But I think the behavior in Kotlin and Java are quite similar.

Here are my two cents :

In Kotlin accessing a lateinit property before it has been initialized throws a special exception that clearly identifies the property being accessed and the fact that it hasn't been initialized. (Reference : https://kotlinlang.org/docs/reference/properties.html).

In Java also, NullPointerException(NPE) is thrown that indicated that property being accessed is Null.

The only difference could be that, NPE is a runtime exception.

about 4 years ago · Santiago Trujillo Denunciar

0

There are many examples here. In your case:

public class MyTest {
    lateinit var subject: TestSubject
}

It compiles to the following Java code:

import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

public final class MyTest {
  @NotNull public TestSubject subject;

  @NotNull
  public final TestSubject getSubject() {
    TestSubject testSubject = this.subject;
    if (testSubject == null) {
      Intrinsics.throwUninitializedPropertyAccessException((String) "subject");
    }
    return testSubject;
  }

  public final void setSubject(@NotNull TestSubject testSubject) {
    Intrinsics.checkParameterIsNotNull((Object) testSubject, (String) "<set-?>");
    this.subject = testSubject;
  }
}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda