java中瞬态变量的任何实时示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21275620/
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
Any real time example for transient variable in java
提问by Shashi
From the question Why does Java have transient fields?. I am able to understand the transient. But, not able to evaluate to using transient keyword at the time of designing.
从问题为什么 Java 有瞬态字段?. 我能够理解瞬态。但是,在设计时无法评估使用transient关键字。
public class A implements Serializable
{
public String s;
public transient ts;
}
If i do the same thing in alternative way..
如果我以另一种方式做同样的事情..
public class A implements Serializable
{
public String s;
//public transient ts;//removing this variable.
}
And use another class and define method inside the class and define the variable ts
and do the operation and persist the value of s as business defines.
并使用另一个类并在类内部定义方法并定义变量ts
并执行操作并按照业务定义持久化 s 的值。
QUESTION
题
I didn't find any real time example in the web where i will take decision to define a variable transient
.
我没有在网上找到任何实时示例,我将决定定义一个变量transient
。
How could i take the decision to define transient at the time of designing? Is there any realtime scenario that helps me to understand?
我如何在设计时决定定义瞬态?是否有任何实时场景可以帮助我理解?
采纳答案by Makoto
To put it plainly: any field marked as transient is not saved if/when the object is serialized.
说白了:如果/当对象被序列化时,任何标记为瞬态的字段都不会被保存。
Let's say, for instance, you have an entity that needs to be serialized, but this entity has a data field that is, in a word, massive:
比方说,例如,您有一个需要序列化的实体,但该实体有一个数据字段,一句话,海量:
public class SerializeMe implements Serializable {
private Integer id;
private String value;
private BigMatrix bigMatrix = BigMatrixFactory.fromValues(id, value).build();
public BigDecimal doExpensiveOperation() {
BigDecimal result = BigDecimal.ZERO;
for(BigDecimal value : bigMatrix.getAllValuesFromAllMatrixFields() {
matrixMultiply(result, value);
}
return result;
}
}
You reallydon't want that BigMatrix
to be serialized, since you'd be serializing a huge quantity of data which isn't necessary to rebuild the object (note, it can be built simply by using the builder pattern, given the ID and value).
你真的不希望它BigMatrix
被序列化,因为你会序列化大量的数据,而这些数据对于重建对象来说是不必要的(注意,它可以简单地通过使用构建器模式来构建,给定 ID 和值)。
In a trivial example, this may not be that big of a deal. But if you were sending this particular entity across a network, that would be a hugedeal - you now have the expense of transferring not only the entity you want, but also some internal state about it that can be generated.
在一个简单的例子中,这可能没什么大不了的。但是,如果您通过网络发送这个特定实体,那将是一笔巨大的交易——您现在不仅要传输所需的实体,还要支付可以生成的有关它的一些内部状态的费用。
回答by Kayaman
If you intend to serialize your objects, but there are fields that aren't strictly a part of the object (such as ones that are for example computed at runtime from other values), those should be transient so they wouldn't be serialized.
如果您打算序列化对象,但有些字段严格来说不是对象的一部分(例如在运行时根据其他值计算的字段),那么这些字段应该是瞬态的,这样它们就不会被序列化。
回答by rachana
A transient variable is a variable that can not be serialized. the transient keyword can be used to indicate the Java virtual machine that the variable is not part of the persistent state of the object.
瞬态变量是不能序列化的变量。瞬态关键字可用于指示 Java 虚拟机该变量不是对象持久状态的一部分。
This can be used in a scenario where only some of the fields in a class are required to be saved and others are actually dervied from the existing .
这可用于仅需要保存类中的某些字段而其他字段实际上来自现有 .
class MyExample implements Serializable
{
private Date currentDate;
private transient String dateValueInString; // this will not be saved
// This methos provides the date in the format 2 feb
private void generatederivedValue()
{
dateValueInString = currentDate.getDay() + " " + convertToStringMonth(currentDate.getMonth());
}
}
So when the object of the above class is serialized , the dateValueInString will not be saved. This would help in saving space when thousands of the objects of this class are serialized.
所以当上面类的对象被序列化时,dateValueInString 将不会被保存。当此类的数千个对象被序列化时,这将有助于节省空间。
回答by Mapsy
As a guideline, you should serialize variables that you wish to be able to restore that you would otherwise be incapable of restoring programmatically. Similarly, you should ensure that you're not saving data unnecessarily; if a variable can be easily repopulated based on processing of other serializable data, you should take that route.
作为准则,您应该序列化您希望能够恢复的变量,否则您将无法以编程方式恢复。同样,您应该确保没有不必要地保存数据;如果可以根据其他可序列化数据的处理轻松地重新填充变量,则应采用该方法。
回答by Autocrab
You can make fields transient if they can be calculated in runtime, which is much cheaper to serialize and deserialize them.
如果字段可以在运行时计算,您可以使它们成为瞬态,这样序列化和反序列化它们的成本要低得多。
回答by rai.skumar
Following cases --
以下案例——
- Declaring attribute as transient is particularly helpful if you have some secured information like password, SSN number etc.
APIs like ArrayList; provides its own serialization mechanism. This can be done by declaring memeber as transient as shown below :
private transient Object[] elementData;
- 如果您有一些安全信息,如密码、SSN 号码等,则将属性声明为瞬态特别有用。
像ArrayList这样的 API ;提供了自己的序列化机制。这可以通过将 memeber 声明为瞬态来完成,如下所示:
私有瞬态对象[] elementData;