美文网首页
静态变量不属于对象,属于类。不能被序列化。还有瞬态的变量也不能被

静态变量不属于对象,属于类。不能被序列化。还有瞬态的变量也不能被

作者: xiaohei_e853 | 来源:发表于2022-03-17 07:33 被阅读0次
package test;

import java.io.*;

public class SerielTest {
    public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException, FileNotFoundException {
        User user = new User();
        user.setAge("22");
        user.setName("小明");
        user.setPassword("admin");
        System.out.println(user.getAge()+"\t"+user.getName()+"\t"+user.getPassword());
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e:/user.txt"));
        user.setAge("33"); //在序列化后在对static修饰的变量进行一次赋值操作
        user.setName("xiaogou");
        oos.writeObject(user);
        oos.flush();
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:/user.txt"));
        User users = (User) ois.readObject();

        System.out.println(users.getAge()+"\t"+users.getName()+"\t"+users.getPassword());

    }
}

class User implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private transient String password;   //被transient修饰的变量
    private static String age;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static String getAge() {
        return age;
    }

    public static void setAge(String age) {
        User.age = age;
    }
}
image.png

这里的age之所以有值,是因为在当前的jvm中方法区有值,所以反序列化的时候有值

package test;

import java.io.*;

class Student1 implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private transient String password;
    private static int count = 0;

    public Student1(String name, String password) {
        System.out.println("调用Student的带参的构造方法");
        this.name = name;
        this.password = password;
        count++;
    }

    public String toString() {
        return "人数: " + count + " 姓名: " + name + " 密码: " + password;
    }
}

public class ObjectSerTest1 {
    public static void main(String args[]) {
        try {

            FileOutputStream fos = new FileOutputStream("test.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            Student1 s1 = new Student1("张三", "12345");
            Student1 s2 = new Student1("王五", "54321");

            oos.writeObject(s1);
            oos.writeObject(s2);

            oos.close();

            FileInputStream fis = new FileInputStream("test.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);

            Student1 s3 = (Student1) ois.readObject();
            Student1 s4 = (Student1) ois.readObject();

            System.out.println(s3);
            System.out.println(s4);

            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

image.png

test.obj ,你打开看看 ,里面是没有
private transient String password;
private static int count=0;
这两个属性的 序列化信息的 ,输出2是因为 在当前jvm实例中count = 2 ;

你可以这样测试,写两个类来测试,一个类写入序列化文件, 另外一个类读出序列化文件

package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Test{


    
    public static void main(String args[]) throws IOException {
        
        try {

            FileInputStream fis = new FileInputStream("test.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);

            Student1 s3 = (Student1) ois.readObject();
            Student1 s4 = (Student1) ois.readObject();

            System.out.println(s3);
            System.out.println(s4);

            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
    }
    
    
    
}
image.png

相关文章

网友评论

      本文标题:静态变量不属于对象,属于类。不能被序列化。还有瞬态的变量也不能被

      本文链接:https://www.haomeiwen.com/subject/hncpdrtx.html