-2

If I have transient variables in a base class (implements serializable) and subclass that extends it, I understand subclasses are also automatically serialized, but should the getters still be required in the base object. I made my vars transient and removed the getters and am getting a PropertyNotFoundException, so I guess I'm answering my own question. Just wanted to know if there was another way to not have to include the getters in the base.

justme
  • 63
  • 5
  • Unclear what you are trying to achieve – Scary Wombat Mar 26 '20 at 04:11
  • just wanted to have something like private transient String myName; and not have the getter for it. I was under the assumption that if a field was transient, it would not be serialized and use it's primitive default, but it looks like it requires the getter as well. – justme Mar 26 '20 at 04:13
  • Getters are absolutely not required. What is this PropertyNotFoundException? – Joni Mar 26 '20 at 04:15
  • are you talking about the `transient` keyword (https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.3) or the java beans `Transient` annotation (https://docs.oracle.com/javase/7/docs/api/java/beans/Transient.html)? – Gus Mar 26 '20 at 04:23
  • Getters don't have anything to do with Java Serialization via the `Serializable` interface you have tagged your question with. Neither does `PropertyNotFoundException`. Unclear what you're asking. – user207421 Mar 26 '20 at 05:50

1 Answers1

2

You are conflating a number of independent issues:

  1. In general, if a field is declared as transient there is no specific need to declare a getters and setters for it.

  2. In general, if you are going to use various frameworks that depend on the JavaBeans conventions (i.e. such things Spring DI, as JPA / Hibernate, the JSP (etc) EL expression language, JAXRS, and so on) then you do need to implement getters and (if needed) setters.

  3. If you are using a persistence mechanism that requires getters or setters, then they are required. But the standard Java Object Serialization mechanism doesn't require this. (It doesn't use getters and setters at all.)

  4. In general, you don't need getters and setters just because inheritance is involved.

You haven't said what is throwing the PropertyNotFoundException, or even given its full class name, so we can't tell you if there is any alternative that doesn't involve adding the getters and setters. However, adding the getters and setters is probably the simplest and most object-oriented solution. See "Why use getters and setters/accessors?" for an explanation.

Note that if you are stuck, and cannot modify the parent class that declares these fields, then you could try add getters and setters in the child class. It might work ... depending on how the framework is implemented.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • My apologies. I was too much in a hurry and did not give enough details. Just FYI, the exception was coming from a BeanResolver on a field I had missed when changing the JSP. Again, I apologize and will not rush these things in the future. – justme Mar 26 '20 at 22:15