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

303
Vistas
What is "private lateinit var lookupKey: String" translated in Java

I am following a tutorial on Android Developer site. It uses Kotlin in the Java example. I have found this post on stack overflow about an equivalent but I do not understand the answers.

The original code is:

// Defines the selection clause
private static final String SELECTION = Data.LOOKUP_KEY + " = ?";
// Defines the array to hold the search criteria
private String[] selectionArgs = { "" };
/*
 * Defines a variable to contain the selection value. Once you
 * have the Cursor from the Contacts table, and you've selected
 * the desired row, move the row's LOOKUP_KEY value into this
 * variable.
 */
private lateinit var lookupKey: String

I have rewritten it as follows:

// Defines the selection clause
private static final String SELECTION = ContactsContract.Data.LOOKUP_KEY + " = ?";

// Defines the array to hold the search criteria
private String[] selectionArgs = { "" };
/*
 * Defines a variable to contain the selection value. Once you
 * have the Cursor from the Contacts table, and you've selected
 * the desired row, move the row's LOOKUP_KEY value into this
 * variable.
 */
private String lookupKey; 

Is this answer too simple? Or is there a more complex translation to java?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

lateinit in Kotlin is just something that plays with the nullability of a variable. Since Java does not have such property, you cannot literally convert lateinit to Java. Well, you can force its type but you won't be able to apply @NonNull/@Nullable.

Lateinit and Lazy are great topics of Kotlin to read about, and in depth. I hope you'll keep learning about those.

The answer was correct: just use private String lookupKey; and... that's it.

BTW, lateinit just creates an if condition in bytecode that will throw if null. You don't have lateinit in Java so that code would need to be make by hand. It's just another nice feature Kotlin has over Java.

over 4 years ago · Santiago Trujillo Denunciar

0

The lateinit var lookupKey in Kotlin defined a property without a value set directly. The value is set to the property later. The compiler takes care to add assertions to make sure it is not possible to read the value before it is not initialized. https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables

The lateinit plays nicely with nullability in Kotlin. So you may define a non-nullable variable without a value instead of having a nullable variable and null checks. https://kotlinlang.org/docs/reference/null-safety.html

The Java code version is not equivalent to the Kotlin version - it misses the check assertion and allows null values

over 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