Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

296
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!