为什么在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:39:33  来源:igfitidea点击:

Why use the `transient` keyword in java?

javaserializationkeywordtransientmodifier

提问by Sitansu

I have an issue related to the transientkeyword's use before the privatemodifier 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 transientkeyword 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& passwordare never get serialize as they are declare as a transient variables.

在上面的例子中dontSaveMe&password永远不会被序列化,因为它们被声明为transient variables.

回答by GGrec

transientkeyword 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 Wrappers, for example, which can contain a lot of business logic).

transient关键字表明该对象不应该被序列化也不应该被持久化。如果您不想序列化重对象(例如Wrappers,它可以包含很多业务逻辑),则可以使用它。

@Transientannotation 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

transientis used to specify which properties of an object will not be saved or serialised. For example, when saving an object to a file, transientspecifies 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 transientattribute (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, transientallows 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 服务的字符串时。