I have an issue related to the transient
keyword's use before the private
modifier in java .
variable declaration:
transient private ResourceBundle pageResourceBundle;
My class looks like this :
public class LoginViewModel extends AbstractViewModel {
transient private ResourceBundle pageResourceBundle;
@AfterCompose
public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
initializeLoginValues();
boolean timeout = BooleanUtils.toBoolean(getHttpServletRequest().getParameter("timeout"));
if (timeout) {
Messagebox.show(pageResourceBundle.getText("MSG_SESSION_HAS_EXPIRED_PLEASE_LOGIN"), pageResourceBundle.getText("LABEL_ALERT"),
Messagebox.OK, Messagebox.ERROR);
}
view.getPage().setTitle(CsdcLicence.get().getApplicationName());
}
I have some questions.
1.why use the transient
keyword before a private variable?
2.what is the purpose of using this keyword?
The transient keyword in Java is used to avoid serialization. If any object of a data structure is defined as a transient , then it will not be serialized. Serialization is the process of converting an object into a byte stream.
The volatile keyword flushes the changes directly to the main memory instead of the CPU cache. On the other hand, the transient keyword is used during serialization. Fields that are marked as transient can not be part of the serialization and deserialization.
A transient variable is a special type of variable which we create by using the transient keyword. It is a special type of variable which have a non-serialized value at the time of serialization. A variable that is initialized by its default value during de-serialization is known as a transient variable.
Serialization is the process of converting an object into a byte stream, and deserialization is the opposite of it. When we mark any variable as transient, then that variable is not serialized.
transient variables are never serialized in java.
It marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred, they are lost intentionally.
please have a look at what serialization is.? and also refer this
Example
public class Foo implements Serializable
{
private String saveMe;
private transient String dontSaveMe;
private transient String password;
//...
}
In above example dontSaveMe
& password
are never get serialize as they are declare as a transient variables
.
And a short use - case:
Imagine exposing a User - Object via a public available webservice.
You sure would like to expose things as nickname, online - state, maybe email or location.
You definitly would not want to expose the password the user uses to login.
Whilst this password could be a property of your User- object,
it should not be serialized e.g. when serializing the object to a JSON - String for the webservice mentioned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With