热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么在java中使用“transient”关键字?(复制)-Whyusethe`transient`keywordinjava?[duplicate]

Thisquestionalreadyhasananswerhere:这个问题已经有了答案:WhydoesJavahavetransientfields

This question already has an answer here:

这个问题已经有了答案:

  • Why does Java have transient fields? 13 answers
  • 为什么Java有瞬变字段?13个答案

I have an issue related to the transient keyword's use before the private modifier in java .

我有一个问题与java中私有修饰符之前的瞬态关键字的使用有关。

variable declaration:

变量声明:

transient private ResourceBundle pageResourceBundle; 

When I Googled it, I found these docs below, but they're talking about serialized. Actually my class does not implement any serialized.

当我在谷歌上搜索的时候,我发现了下面的这些文档,但是它们是序列化的。实际上,我的类没有实现任何序列化。

For More info:

更多信息:

http://java91.blogspot.in/2017/01/why-does-java-have-transient-fields.html

http://java91.blogspot.in/2017/01/why-does-java-have-transient-fields.html

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。为什么在私有变量之前使用瞬态关键字?

2.what is the purpose of using this keyword?

2。使用这个关键字的目的是什么?

5 个解决方案

#1


29  

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.

当成员变量被持久化到字节流时,它标记为不被序列化。当一个对象通过网络传输时,该对象需要被“序列化”。序列化将对象状态转换为串行字节。这些字节通过网络发送,对象从这些字节中重新创建。由java临时关键字标记的成员变量没有被转移,它们是故意丢失的。

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.

在上面的例子中,dontSaveMe和password永远不会被序列化,因为它们是作为瞬态变量声明的。

#2


15  

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服务公开一个用户对象。你肯定会想公开一些东西,比如昵称、在线状态、电子邮件或位置。您肯定不想公开用户登录时使用的密码。虽然这个密码可能是您的用户对象的属性,但是它不应该被序列化,例如,当您为上面提到的webservice将对象序列化为JSON - String时。

#3


6  

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

瞬态关键字建议对象不应该被序列化,也不应该被持久化。如果不希望序列化重要对象(例如包装器,它可以包含很多业务逻辑),可以使用它。

@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. :-)

我已经包含了注释解释,因为我记得我被这两个弄糊涂了。:-)

#4


5  

  • Transient keyword can only be applied to member variables.
    Applying it to method or local variable is compilation error.

    瞬态关键字只能应用于成员变量。将它应用于方法或局部变量是编译错误。

  • The keyword transient in Java used to indicate that the variable
    should not be serialized. By default all the variables in the object are converted to persistent state. In some cases, you may want to
    avoid persisting some variables because you don’t have the necessity to transfer across the network. So, you can declare those variables
    as transient.

    Java中用于指示不应该序列化变量的关键字transient。默认情况下,对象中的所有变量都被转换为持久状态。在某些情况下,您可能希望避免持久化一些变量,因为您没有必要跨网络传输。你可以把这些变量声明为瞬态。

#5


3  

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会指定在将对象保存到文件中时,不会将哪些属性或属性保存到该文件中。

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.

当对象从文件中重新创建时,该临时属性(或私有属性)的值将不会被重新创建,因为它从未被保存或序列化到该文件中。在某些情况下,您可能希望避免持久化这些私有实例变量或对象的属性,瞬态允许您这样做。


推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 如何解决《C中数组中的关键字》经验,为你挑选了1个好方法。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  •    今天开始,夜风将在每篇文章中解释几个标签,让大家一目了然的学习:1) 故名思意,这是一篇HTML文档的类型,为了定义每篇文档用什么代码规范,通常必须放在文 ... HTML标签天天练1">[详细]
  • 如何解决《AndroidStudio自定义关键字外观着色》经验,求大佬解答? ... [详细]
author-avatar
lovely月夜静悄悄知_302
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有