Java: Static transient fields

前端 未结 2 1392
抹茶落季
抹茶落季 2021-01-31 14:11

I just found out in Java you can declare a field \'static transient\' - the compiler doesn\'t complain. This doesn\'t seem to be useful in any way since static fields are not se

2条回答
  •  眼角桃花
    2021-01-31 15:12

    In most cases, it is not useful. Static fields are indeed not serialized by the default serializer.

    However, static transient fields can be detected via reflection. If someone writes its own serializer and he wants to also serialize static fields, then he might take the transient keyword in consideration and skip the serialization of that particular field.

    PS: This answer is posted for the sake of completeness, and is based on Peter Lawrey's comment. Credits to him.

提交回复
热议问题