Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

365
Visualizações
Why @Transient can't be used with val fields?

I wrote data class

data class FileHeader(
    val relativePath: String,
    val orderNumber: Long,
    val bodySize: Int
) : Serializable {
@Transient
var headerSize: Int = 0
    get() = relativePath.length + 8
}

It works as i expect.

But why i can't use @Transient with val field?

The error is:

This annotation is not applicable to target member property without backing field or delegate

Are there any reasons why it implemented in this way?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The annotation

Marks the JVM backing field of the annotated property as transient, meaning that it is not part of the default serialized form of the object.

The default serialization works on fields and doesn't care about getter methods. So if there's no backing field, there's nothing to serialize (and nothing to mark as transient in bytecode). The annotation would be useless in this case, so the designers chose to make it an error.

If you don't see why there's no backing field:

A backing field will be generated for a property if it uses the default implementation of at least one of the accessors, or if a custom accessor references it through the field identifier.

With your var, the backing field is needed by the default setter; when you change it to val, it isn't.

over 4 years ago · Santiago Trujillo Relatório

0

Try this

@get:javax.persistence.Transient
val headerSize
  get() { ... }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda