热门标签 | 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.

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


推荐阅读
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细记录了在基于Debian的Deepin 20操作系统上安装MySQL 5.7的具体步骤,包括软件包的选择、依赖项的处理及远程访问权限的配置。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • Yii 实现阿里云短信发送 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
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社区 版权所有