Serializable

์ง๋ ฌํ™”๋ผ๋Š” ์šฉ์–ด์— ๋Œ€ํ•ด์„œ ๋“ค์–ด๋งŒ ๋ณด๊ณ  ๊ณต๋ถ€ํ•ด๋ณธ ์ ์€ ์—†๋Š”๋ฐ ์ด๋ฒˆ ๊ธฐํšŒ์— ์ •๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค,, ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ์ง๋ ฌํ™”์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

public interface Serializable {
}

Serializable์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ณด๋ฉด ๋ฉ”์†Œ๋“œ๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋Ÿฐ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ๋ฉ”์†Œ๋“œ๋„ ์—†๋Š” ์ด ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋„๋Œ€์ฒด ์™œ ์žˆ๋Š” ๊ฒƒ์ผ๊นŒ์š”?

๊ฐœ๋ฐœ์„ ํ•˜๋‹ค ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

  • ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•  ์ผ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ €์žฅํ•œ ๊ฐ์ฒด๋ฅผ ์ฝ์„ ์ผ์ด ์ƒ๊ธธ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋‹ค๋ฅธ ์„œ๋ฒ„์—์„œ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ›์„ ์ผ๋„ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿด ๋•Œ ๊ผญ ํ•„์š”ํ•œ ๊ฒƒ์ด Serializable ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ํด๋ž˜์Šค๊ฐ€ ํŒŒ์ผ์— ์ฝ๊ฑฐ๋‚˜ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ฑฐ๋‚˜, ๋‹ค๋ฅธ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๊ฑฐ๋‚˜ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด JVM์—์„œ ํ•ด๋‹น ๊ฐ์ฒด๋Š” ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.

๊ทธ๋ž˜์„œ ์ง๋ ฌํ™”๊ฐ€ ๋ฌด์—‡์ธ๊ฐ€?

  • ์ž๋ฐ” ์ง๋ ฌํ™”๋ž€ ์ž๋ฐ” ์‹œ์Šคํ…œ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์™ธ๋ถ€์˜ ์ž๋ฐ” ์‹œ์Šคํ…œ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐ”์ดํŠธ(byte) ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ ๊ณผ ๋ฐ”์ดํŠธ๋กœ ๋ณ€ํ™˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ (์—ญ์ง๋ ฌํ™”)์„ ์•„์šธ๋Ÿฌ์„œ ์ด์•ผ๊ธฐํ•ฉ๋‹ˆ๋‹ค.

  • ์‹œ์Šคํ…œ์ ์œผ๋กœ ์ด์•ผ๊ธฐํ•˜์ž๋ฉด JVM(Java Virtual Machine ์ดํ•˜ JVM)์˜ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์ฃผ(ํž™ ๋˜๋Š” ์Šคํƒ)๋˜์–ด ์žˆ๋Š” ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ ๊ณผ ์ง๋ ฌํ™”๋œ ๋ฐ”์ดํŠธ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ด์„œ JVM์œผ๋กœ ์ƒ์ฃผ์‹œํ‚ค๋Š” ํ˜•ํƒœ๋ฅผ ๊ฐ™์ด ์ด์•ผ๊ธฐํ•ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ ์—์„œ ์ฐธ๊ณ ํ•œ ์ •์˜์ž…๋‹ˆ๋‹ค.

Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋“ค์„ ๋ณด๋ฉด serialVersionUID๋ผ๋Š” ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋ณธ ์ ์ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {

    private static final long serialVersionUID = 362498820763181265L;
}

์˜ˆ๋ฅผ๋“ค์–ด, HashMap ํด๋ž˜์Šค๋ฅผ ๋ณด๋ฉด ์œ„์™€ ๊ฐ™์€ ๋ณ€์ˆ˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํ›„์—๋Š” ์œ„์™€ ๊ฐ™์ด serialVersionUID๋ผ๋Š” ๊ฐ’์„ ์ง€์ •ํ•ด ์ฃผ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. (๋งŒ์•ฝ ๋ณ„๋„๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด, ์ž๋ฐ” ์†Œ์Šค๊ฐ€ ์ปดํŒŒ์ผ๋  ๋•Œ ์ž๋™์œผ๋กœ ์ƒ๊น๋‹ˆ๋‹ค.)

static final long serialVersionUID = 1L;

์œ„์™€ ๊ฐ™์ด ๋ฐ˜๋“œ์‹œ static final long์œผ๋กœ ์„ ์–ธํ•ด์•ผ ํ•˜๋ฉฐ, ๋ณ€์ˆ˜๋ช…๋„ serialVersionUID๋กœ ์„ ์–ธํ•ด ์ฃผ์–ด์•ผ ์ž๋ฐ”์—์„œ ์ธ์‹์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ด ๊ฐ’์€ ์–ด๋””์— ์‚ฌ์šฉ๋˜๊ณ  ์–ด๋–ค ๊ฐ’์„ ๋„ฃ์–ด์•ผ ํ• ๊นŒ์š”?

  • ๊ฐ’์€ ์•„๋ฌด๋Ÿฐ ๊ฐ’์ด๋‚˜ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๊ฐ’์˜ ์˜๋ฏธ๋Š” ํ•ด๋‹น ๊ฐ์ฒด์˜ ๋ฒ„์ „์„ ๋ช…์‹œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

A๋ผ๋Š” ์„œ๋ฒ„์—์„œ B๋ผ๋Š” ์„œ๋ฒ„๋กœ SerialDTO๋ผ๋Š” ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ „์†กํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์†กํ•˜๋Š” A ์„œ๋ฒ„์— SerialDTO๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๊ณ , ์ „์†ก๋ฐ›๋Š” B ์„œ๋ฒ„์—๋Š” SerialDTO๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ๋งŒ ๊ทธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด์ž„์„ ์•Œ๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ A ์„œ๋ฒ„๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” SerialDTO์—๋Š” ๋ณ€์ˆ˜๊ฐ€ 3๊ฐœ ์žˆ๊ณ , B ์„œ๋ฒ„์˜ SerialDTO์—๋Š” ๋ณ€์ˆ˜๊ฐ€ 4๊ฐœ ์žˆ๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ์ด๋Ÿฌ๋ฉด ์ž๋ฐ”์—์„œ๋Š” ์ œ๋Œ€๋กœ ์ฒ˜๋ฆฌ๋ฅผ ๋ชปํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ ์„œ๋ฒ„๊ฐ€ ์‰ฝ๊ฒŒ ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ๊ฐ™์€์ง€ ๋‹ค๋ฅธ์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” serialVersionUID๋กœ ๊ด€๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰ ํด๋ž˜์Šค ์ด๋ฆ„์ด ๊ฐ™๋”๋ผ๋„ ์ด ID๊ฐ€ ๋‹ค๋ฅด๋ฉด ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ผ๊ณ  ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€, ๊ฐ™์€ UID๋ผ๊ณ  ํ• ์ง€๋ผ๋„, ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋‚˜ ํƒ€์ž… ๋“ฑ์ด ๋‹ค๋ฅด๋ฉด ์ด ๊ฒฝ์šฐ๋„ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค.

๊ธ€๋งŒ ๋ด์„œ๋Š” ์‰ฝ์ง€๊ฐ€ ์•Š์€๋ฐ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์กฐ๊ธˆ๋งŒ ์žˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ๋” ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ฐ์ฒด๋ฅผ ์ €์žฅํ•ด๋ณด๊ธฐ

import java.io.Serializable;

public class SerialDTO implements Serializable {
    private String booName;
    private int bookOrder;
    private boolean bestSeller;
    private long soldPerDay;

    public SerialDTO(String booName, int bookOrder, boolean bestSeller, long soldPerDay) {
        this.booName = booName;
        this.bookOrder = bookOrder;
        this.bestSeller = bestSeller;
        this.soldPerDay = soldPerDay;
    }

    @Override
    public String toString() {
        return "SerialDTO{" +
                "booName='" + booName + '\'' +
                ", bookOrder=" + bookOrder +
                ", bestSeller=" + bestSeller +
                ", soldPerDay=" + soldPerDay +
                '}';
    }
}

DTO ํด๋ž˜์Šค๋ฅผ ์ €์žฅํ•ด๋ณด๋Š” ์˜ˆ์ œ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class ManageObject {
    public static void main(String[] args) {
        ManageObject manage = new ManageObject();
        String fullPath = "/Users/choejeong-gyun/Documents/test.md";

        SerialDTO dto = new SerialDTO("God of Java", 1, true, 100);
        manage.saveObject(fullPath, dto);
    }

    public void saveObject(String fullPath, SerialDTO dto) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(fullPath);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(dto);
            System.out.println("Write Success");
        } catch (Exception e) { 
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • ์ž๋ฐ”์—์„œ๋Š” ObjectOutputStream ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ObjectInputStream์„ ์‚ฌ์šฉํ•˜๋ฉด ์ €์žฅํ•ด๋†“์€ ๊ฐ์ฒด๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์œ„์˜ ์ฝ”๋“œ์—์„œ๋„ FileOutputStream ๊ฐ์ฒด๋ฅผ ๋งŒ๋“  ํ›„์— ObjectOutputStream์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒผ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๋Š” ํŒŒ์ผ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

  • writeObject()๋ฅผ ํ†ตํ•ด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜์–ด์˜จ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํŒŒ์ผ์„ ํ™•์ธํ•ด๋ณด๋ฉด ํŒŒ์ผ์— ๊ฐ์ฒด๊ฐ€ ์ €์žฅ์ด ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ์ฒด๋ฅผ ์ฝ์–ด๋ณด๊ธฐ

import java.io.*;

public class ManageObject {
    public static void main(String[] args) {
        ManageObject manage = new ManageObject();
        String fullPath = "/Users/choejeong-gyun/Documents/test.md";
        manage.loadObject(fullPath);
    }

    public void loadObject(String fullPath) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(fullPath);
            ois = new ObjectInputStream(fis);
            Object obj = ois.readObject();
            SerialDTO dto = (SerialDTO)obj;
            System.out.println(dto);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
SerialDTO{booName='God of Java', bookOrder=1, bestSeller=true, soldPerDay=100}

๊ทธ๋Ÿฌ๋ฉด ์œ„์™€ ๊ฐ™์ด ํŒŒ์ผ์— ์ €์žฅ๋œ ๊ฐ์ฒด ์ •๋ณด๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฒˆ์— SerialDTO ํด๋ž˜์Šค์˜ ํ•„๋“œ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•œ ํ›„์— ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์‹คํ–‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

java.io.InvalidClassException: FileIO.SerialDTO; local class incompatible: stream classdesc serialVersionUID = -358710248991570103, local class serialVersionUID = 1424372278057927306
    at java.base/java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:689)
    at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1982)
    at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1851)
    at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2139)
    at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1668)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:482)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:440)
    at FileIO.ManageObject.loadObject(ManageObject.java:49)
    at FileIO.ManageObject.main(ManageObject.java:12)

๊ทธ๋Ÿฌ๋ฉด ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด serialVersionUID ๊ฐ’์ด ๋‹ค๋ฅด๋‹ค๋Š” ์—๋Ÿฌ ๋ฉ”์„ธ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ์ฒด ํ˜•ํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ปดํŒŒ์ผ์‹œ serialVersionUID๊ฐ€ ๋‹ค์‹œ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

transient๋ผ๋Š” ์˜ˆ์•ฝ์–ด๋Š” Serializable๊ณผ ๋–จ์–ด์งˆ ์ˆ˜ ์—†๋Š” ๊ด€๊ณ„

transient private int bookOrder;

SerialDTO ํด๋ž˜์Šค์— transient ๋ผ๋Š” ์˜ˆ์•ฝ์–ด๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„์— ๋‹ค์‹œ ๊ฐ์ฒด๋ฅผ ํŒŒ์ผ์— ์ €์žฅํ•˜๊ณ  ์ฝ์–ด์˜ค๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

Write Success
SerialDTO{booName='God of Java', bookOrder=0, bestSeller=true, soldPerDay=100}

๊ทธ๋Ÿฌ๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ bookOrder์— 1์„ ๋„ฃ์—ˆ์ง€๋งŒ ๊ฒฐ๊ณผ์—๋Š” 0์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

0์ด ๋‚˜์˜ค๋Š” ์ด์œ ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”?

๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜, ๋‹ค๋ฅธ JVM์œผ๋กœ ๋ณด๋‚ผ ๋•Œ, transient ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋Š” Serializable์˜ ๋Œ€์ƒ์—์„œ ์ œ์™ธ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ๋ญํ•˜๋Ÿฌ ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋‚˜ ์‹ถ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ํŒจ์Šค์›Œ๋“œ์™€ ๊ฐ™์ด ๋ณด์•ˆ์ƒ ์ค‘์š”ํ•œ ๋ณ€์ˆ˜๋‚˜ ๊ผญ ์ €์žฅํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ๋Š” transient๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ง๋ ฌํ™” ์ฐธ๊ณ ํ•˜๊ธฐ

public class SuperUserInfo implements Serializable {
    String name;
    String password;
}
public class UserInfo extends SuperUserInfo {
    int age;
}

์ด๋Ÿฌํ•œ ์ƒ์† ๊ด€๊ณ„๊ฐ€ ์žˆ์„ ๋•Œ, SuperUserInfo ํด๋ž˜์Šค๋ฅผ ์ง๋ ฌํ™” ํ–ˆ์ง€๋งŒ, ํ•˜์œ„ ํด๋ž˜์Šค์ธ UserInfo ํด๋ž˜์Šค๋„ ์ง๋ ฌํ™”๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. UserInfo๋ฅผ ์ง๋ ฌํ™”ํ•˜๋ฉด ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ name, password๋„ ๊ฐ™์ด ์ง๋ ฌํ™”๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

public class SuperUserInfo {
    String name;
    String password;
}
public class UserInfo extends SuperUserInfo implements Serializable {
    int age;
}

ํ•˜์ง€๋งŒ ์œ„์™€ ๊ฐ™์ด ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ์ง๋ ฌํ™”๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ง๋ ฌํ™”ํ•  ๋•Œ name, password๋Š” ์ง๋ ฌํ™” ๋Œ€์ƒ์—์„œ ์ œ์™ธ๋ฉ๋‹ˆ๋‹ค.

public class UserInfo implements Serializable {
    int age;

    Object object = new Object();  // Object ๊ฐ์ฒด๋Š” ์ง๋ ฌํ™”ํ•  ์ˆ˜ ์—†๋‹ค. 
}

์œ„์˜ ์ฝ”๋“œ์—์„œ UserInfo ํด๋ž˜์Šค๋Š” Serializable์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์–ด์„œ ์ง๋ ฌํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ง๋ ฌํ™”๋ฅผ ์‹œ๋„ํ•˜๋ฉด java.io.NotSerializableException์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ด์œ ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”? ๋ฐ”๋กœ Object ๊ฐ์ฒด ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ์—์„œ ๋ณด์•˜๋“ฏ์ด ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ Serializable์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์ž์‹ ํด๋ž˜์Šค๋„ ์ง๋ ฌํ™”๊ฐ€ ๊ฐ€๋Šฅํ–ˆ์Šต๋‹ˆ๋‹ค.

Object๋Š” ๋ชจ๋“  ํด๋ž˜์Šค์˜ ์ตœ๊ณ  ์กฐ์ƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ํด๋ž˜์Šค๊ฐ€ Serializable์„ ๊ตฌํ˜„ํ•˜๋‹ค๋ฉด ๋ชจ๋“  ํด๋ž˜์Šค๋“ค์ด ์ง๋ ฌํ™”๊ฐ€ ๊ฐ€๋Šฅํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— Object ํด๋ž˜์Šค๋Š” Serializable์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„ ์ง๋ ฌํ™”๋ฅผ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

public class UserInfo implements Serializable {
    int age;

    Object object = new String("abc");  
}

ํ•˜์ง€๋งŒ ์œ„์™€ ๊ฐ™์ด ๋‹คํ˜•์„ฑ์„ ์ด์šฉํ•œ ์ฝ”๋“œ๋Š” ์ง๋ ฌํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ํƒ€์ž…์€ Object์˜ ํƒ€์ž…์ด์ง€๋งŒ ์‹ค์ œ๋กœ ์ €์žฅ๋œ ๊ฐ์ฒด๋Š” ์ง๋ ฌํ™”๊ฐ€ ๊ฐ€๋Šฅํ•œ String ์ธ์Šคํ„ด์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ์ง๋ ฌํ™”๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด ์•„๋‹Œ ์‹ค์ œ๋กœ ์—ฐ๊ฒฐ๋œ ๊ฐ์ฒด์˜ ์ข…๋ฅ˜์— ์˜ํ•ด์„œ ๊ฒฐ์ •๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋‘๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Reference

์ถœ์ฒ˜ : https://devlog-wjdrbs96.tistory.com/268

Last updated