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

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


推荐阅读
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 本文介绍了使用postman进行接口测试的方法,以测试用户管理模块为例。首先需要下载并安装postman,然后创建基本的请求并填写用户名密码进行登录测试。接下来可以进行用户查询和新增的测试。在新增时,可以进行异常测试,包括用户名超长和输入特殊字符的情况。通过测试发现后台没有对参数长度和特殊字符进行检查和过滤。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • 数字账号安全与数据资产问题的研究及解决方案
    本文研究了数字账号安全与数据资产问题,并提出了解决方案。近期,大量QQ账号被盗事件引起了广泛关注。欺诈者对数字账号的价值认识超过了账号主人,因此他们不断攻击和盗用账号。然而,平台和账号主人对账号安全问题的态度不正确,只有用户自身意识到问题的严重性并采取行动,才能推动平台优先解决这些问题。本文旨在提醒用户关注账号安全,并呼吁平台承担起更多的责任。令牌云团队对此进行了长期深入的研究,并提出了相应的解决方案。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
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社区 版权所有