В это задаче нужно было указать, что поле pattern transient . Но почему? Я понимаю зачем transient не указываем в полях currentDate и temperature . Там нам нет смысла сохранять состояние полей. Дата изменится , а температуру мы вообще должны получить заново как параметр в конструкторе .
А pattern - он же может не меняться. Он же паттерн и логично чтобы он сохранялся.
Но валидатор говорит что поле паттерн должно быть transient . Что не так в моих рассуждениях?
package com.javarush.task.task20.task2014;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
Serializable Solution
*/
public class Solution implements Serializable {
public static void main(String[] args) {
System.out.println(new Solution(4));
}
private final String pattern = "dd MMMM yyyy, EEEE";
private transient Date currentDate;
private transient int temperature;
String string;
public Solution(int temperature) {
this.currentDate = new Date();
this.temperature = temperature;
string = "Today is %s, and current temperature is %s C";
SimpleDateFormat format = new SimpleDateFormat(pattern);
this.string = String.format(string, format.format(currentDate), temperature);
}
@Override
public String toString() {
return this.string;
}
}