Java transient关键字的理解
1. transient的作用及使用方法
package test; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable; public class SerializableTest { public static void main(String[] args) throws IOException, ClassNotFoundException {
FileOutputStream fos = new FileOutputStream("game-person.info");
ObjectOutputStream oos = new ObjectOutputStream(fos);
GamePerson personIn = new GamePerson();
personIn.setName("abcde");
personIn.setLevel(1);
personIn.setForceValue(2);
personIn.setDefenseValue(3);
personIn.setMoney(200);
// 通过在序列化之前,使用类变量直接赋值,可知
// 一个静态变量不管是否被transient修饰,均不能被序列化),反序列化后类中static型变量username的值为当前JVM中
// 对应static变量的值,这个值是JVM中的不是反序列化得出的
GamePerson.level = 300;
oos.writeObject(personIn);
oos.flush();
oos.close(); FileInputStream fis = new FileInputStream("game-person.info");
ObjectInputStream ois = new ObjectInputStream(fis);
GamePerson personOut = (GamePerson) ois.readObject();
System.out.println(personOut.getName());
System.out.println(personOut.getLevel());
System.out.println(personOut.getForceValue());
System.out.println(personOut.getDefenseValue());
System.out.println(personOut.getMoney());
}
} class GamePerson implements Serializable { private static final long serialVersionUID = 1L; private String name;
public static int level;
private int forceValue;
private int defenseValue;
// 金额是敏感数据:不对此属性进行序列化
private transient long money; public long getMoney() {
return money;
} public void setMoney(long money) {
this.money = money;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getLevel() {
return level;
} public void setLevel(int level) {
this.level = level;
} public int getForceValue() {
return forceValue;
} public void setForceValue(int forceValue) {
this.forceValue = forceValue;
} public int getDefenseValue() {
return defenseValue;
} public void setDefenseValue(int defenseValue) {
this.defenseValue = defenseValue;
} }

2. transient使用小结

3. transient使用细节——被transient关键字修饰的变量真的不能被序列化吗?
import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream; /**
* @descripiton Externalizable接口的使用
*
* @author Alexia
* @date 2013-10-15
*
*/
public class ExternalizableTest implements Externalizable { private transient String content = "是的,我将会被序列化,不管我是否被transient关键字修饰"; @Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(content);
} @Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
content = (String) in.readObject();
} public static void main(String[] args) throws Exception { ExternalizableTest et = new ExternalizableTest();
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
new File("test")));
out.writeObject(et); ObjectInput in = new ObjectInputStream(new FileInputStream(new File(
"test")));
et = (ExternalizableTest) in.readObject();
System.out.println(et.content); out.close();
in.close();
}
}
Java transient关键字的理解的更多相关文章
- java transient关键字作用,使用场景。
java transient关键字作用,使用场景. 2016年08月31日 15:31:10 阅读数:4280 transient的作用及使用方法,官方解释为: Variables may be ma ...
- 【Java编程】volatile和transient关键字的理解
理解volatile volatile是java提供的一种轻量级的同步机制,所谓轻量级,是相对于synchronized(重量级锁,开销比较大)而言的. 根据java虚拟机的内存模型,我们知道 ...
- Java transient关键字使用小记
哎,虽然自己最熟的是Java,但很多Java基础知识都不知道,比如transient关键字以前都没用到过,所以不知道它的作用是什么,今天做笔试题时发现有一题是关于这个的,于是花个时间整理下transi ...
- Java transient关键字序列化时使用小记
1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过 ...
- Java transient关键字使用小结
1.transient关键字只能修饰变量,而不能修饰方法和类.注意,本地变量是不能被transient关键字修饰的.2.被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被t ...
- (转)Java transient关键字使用小记
背景:最近在看java底层的源码实现,看到一个关键字,不是很熟悉,专门做个记录. 原文出处:http://www.importnew.com/21517.html#comment-637072 哎,虽 ...
- 【转】Java transient关键字使用小记
哎,虽然自己最熟的是Java,但很多Java基础知识都不知道,比如transient关键字以前都没用到过,所以不知道它的作用是什么,今天做笔试题时发现有一题是关于这个的,于是花个时间整理下transi ...
- [转]Java transient关键字
java 的transient关键字的作用是需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中. trans ...
- Java transient关键字使用小记(转)
哎,虽然自己最熟的是Java,但很多Java基础知识都不知道,比如transient关键字以前都没用到过,所以不知道它的作用是什么,今天做笔试题时发现有一题是关于这个的,于是花个时间整理下transi ...
随机推荐
- swift - 各种手势用法大全
UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有: 1.首先创建几个view,来用于手势的检测 let view1 = UIView() let view2 ...
- C语言近程型(near)和远程型(far)的区别是什么?
DOS用一种分段结构来寻址计算机的内存,每一个物理存储位置都有一个可以用段一偏移量方式来访问的相关地址.例如,下面就是一个典型的段式地址: A000:1234 冒号左边的部分代表段地址(A00 ...
- js方法随机抽取n个随机数
function getImageRandomPosition(){ do { var n = Math.floor(Math.random() * 12);//n为随机出现的0-11之内的数值 fo ...
- 点击一个textView里的link导航至程序内可返回的自定义webView
1,在AppDelegate.h里定义一个 id currentViewController; 在AppDelegate.m里 @implementation UIApplication (Priva ...
- Facebook广告目标摘要
Facebook Ads look slightly different depending on the results you want. This guide includes recommen ...
- 小程序 - API 踩坑记录(更新中...)
API 小程序API结构导览图: 声明: 请尊重博客园原创精神,转载或使用图片请注明: 博主:xing.org1^ 出处:http://www.cnblogs.com/padding1015/
- Redis单机主从高可用性优化
版权声明:本文由陈龙原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/127 来源:腾云阁 https://www.qclou ...
- 【BZOJ1458】士兵占领 最小流
[BZOJ1458]士兵占领 Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占 ...
- java的Result类
import org.apache.commons.lang.StringUtils; import java.io.Serializable;import java.util.HashMap;imp ...
- windows服务的默认启动类型和登录帐户
转自:http://www.winhelponline.com/blog/windows-7-services-default-startup-type/ Service Name Startup T ...