为什么在java中使用`transient`关键字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20700530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why use the `transient` keyword in java?
提问by Sitansu
I have an issue related to the transient
keyword's use before the private
modifier in java .
我有一个与transient
关键字private
在 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?
1.为什么transient
在私有变量之前使用关键字?
2.what is the purpose of using this keyword?
2.使用这个关键字的目的是什么?
采纳答案by rachana
transient variables are never serialized in java.
瞬态变量永远不会在 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.
当它被持久化到字节流时,它标记一个成员变量不被序列化。当一个对象通过网络传输时,该对象需要被“序列化”。序列化将对象状态转换为串行字节。这些字节通过网络发送,并从这些字节重新创建对象。javatransient关键字标记的成员变量不转移,故意丢失。
please have a look at what serializationis.? 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
.
在上面的例子中dontSaveMe
&password
永远不会被序列化,因为它们被声明为transient variables
.
回答by GGrec
transient
keyword suggests that the object should not be serialized, nor persisted. You can use it if you don't want to serialize heavy objects (such as Wrapper
s, for example, which can contain a lot of business logic).
transient
关键字表明该对象不应该被序列化,也不应该被持久化。如果您不想序列化重对象(例如Wrapper
s,它可以包含很多业务逻辑),则可以使用它。
@Transient
annotation suggests that the object should not be persisted(if you've been playing with Hibernate, for example), but it can be serialized.
@Transient
注释表明该对象不应被持久化(例如,如果您一直在使用 Hibernate),但它可以被序列化。
I've included the annotation explanation, because I remember being confused by the two. :-)
我已经包括了注释解释,因为我记得被两者混淆了。:-)
回答by blackpanther
transient
is used to specify which properties of an object will not be saved or serialised. For example, when saving an object to a file, transient
specifies which properties or attributes will not be saved to that file when that object is saved to a file.
transient
用于指定对象的哪些属性将不被保存或序列化。例如,将对象transient
保存到文件时,指定将对象保存到文件时不将哪些属性或属性保存到该文件中。
When the object is re-created from the file, the value of that transient
attribute (or private property) will not be re-created as it was never saved, or serialised to that file. In some cases, you may want to avoid persisting some of these private instance variables or attributes of an object, transient
allows you to do that.
从文件重新创建对象时,该transient
属性(或私有属性)的值将不会重新创建,因为它从未保存或序列化到该文件。在某些情况下,您可能希望避免保留某些私有实例变量或对象的属性,transient
允许您这样做。
回答by Peter
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.
还有一个简短的用例:
想象一下通过公共可用的 Web 服务公开一个用户 - 对象。您肯定希望将事物公开为昵称、在线状态、电子邮件或位置。您绝对不想公开用户用于登录的密码。虽然此密码可能是您的用户对象的属性,但它不应被序列化,例如在将对象序列化为 JSON - 提到的 Web 服务的字符串时。