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

396
Vistas
How to hide Kotlin's lateinit var backing field from Java?

In Kotlin, suppose, I have class:

class MyKotlinClass {
    lateinit var field: String
}

According to docs:

Late-Initialized properties are also exposed as fields. The visibility of the field will be the same as the visibility of lateinit property setter.

I can use in java code either myKotlinClass.field or myKotlinClass.getField(). I want to disable field access and remain only access through getter and setter.

How can I achieve this and remain lateinit modifier?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use @JvmSynthetic that hides declarations from Java (and not from Kotlin). Just annotate the backing field of the property:

@field:JvmSynthetic
lateinit var field: String

Though the field will remain public in the bytecode, it will also have the synthetic modifier, which prevents it from being used in Java sources. However, the field seems to be still accessible through reflection at runtime.

See also: another question about @JvmSynthetic (though no definite answer there).

about 4 years ago · Santiago Trujillo Denunciar

0

The classical solution to this problem would be to use property delegation:

import kotlin.properties.Delegates

class MyKotlinClass {
    var field: String by Delegates.notNull()
}

This code does exactly what you asked for in the question

about 4 years ago · Santiago Trujillo Denunciar

0

The visibility of lateinit field is derived from the visibility of setter of the corresponding property, so another option is to have a property with non-public setter:

lateinit var field: String
    private set

The drawback of this approach is that the setter itself becomes inaccessible outside of the class.

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