m.blog.naver.com/PostView.nhn?blogId=kkson50&logNo=220564174258&proxyReferer=https:%2F%2Fwww.google.com%2F

 

1) serialization: 객체를 바이트 형식으로 전환시켜 DB, memory, file 에 저장했다가 나중에 deserialization을 통해 객체를 만들어서 사용하는것

deserialization: 

 

2) 객체 형태로 들어갔다가 객체 형태로 나와야 하니까, 

FileOutputStream() => ObjectOutputStream() 

 

3) serialVersionUID로 특정 객체를 인식하는데에 사용한다.

m.blog.naver.com/PostView.nhn?blogId=kkson50&logNo=220564273220&proxyReferer=https:%2F%2Fwww.google.com%2F

위 블로그 보기) 이렇듯 serialVersionUID값을 파일등으로 저장을 할때 해당하는 클래스의 버전이 맞는지를 확인하는 중요한 장치입니다.

그런데 이 serialVersionUID를 작성하지 않으면 Java VM에서 내부 알고리즘에 따라서 자동으로 작성을 하게 되는데, 이것은 어떤 Java VM을 사용하는지에 따라서 달라지게 됩니다.

따라서 무조건 serialVersionUID 값을 설정하기를 권장을 합니다.

 

 

Points to remember:

1) static으로 선언되지 않은 변수들만 serialization 을 통해 저장이 된다.

2) Static 멤버와 transient 멤버는 Serialization을 할때 저장되지 않는다. 그래서 static으로 선언하지 않은 변수들 중에 저장하고 싶지 않은 내용은 무조건 modifier를 transient으로 만들어줘라.

3) 만약 부모 클래스가 Serializable 인터페이스를 implement했다면, 자식 클래스는 할 필요가 없다. 하지만 자식 클래스가 Serializable을 사용하고자 한다면, 부모 클래스는 무조건 Serializable 인터페이스를 먼저 implement해야만 subclass(자식)클래스가 사용할 수 있다.

4) 객체가 Deserialization을 거칠때 constructor는 실행되지 않는다.

5) 


1. If a parent class has implemented Serializable interface then child class doesn’t need to implement it but vice-versa is not true.
2. Only non-static data members are saved via Serialization process.
3. Static data members and transient data members are not saved via Serialization process.So, if you don’t want to save value of a non-static data member then make it transient.
4. Constructor of object is never called when an object is deserialized.
5. Associated objects must be implementing Serializable interface.
Example :

 

--

www.geeksforgeeks.org/serialization-in-java/

가령 a변수를 transient, ㅠb변수를 static으로 선언했다고 쳐보자.

Serialization 과정에서 transient변수는 serialize가 안되고, deserialization과정에서 default 값으로 세팅이 된다. int는 0, 객체는 null로...)

Serialization 과정에서 static변수는 serialize가 안되고, deserialization과정에서 현재 상태의 클래스 멤버변수가 가지는 값으로 deserialize되어 세팅이 된다. 

 

You have seen while deserializing the object the values of a and b has changed. The reason being a was marked as transient and b was static.
In case of transient variables:- A variable defined with transient keyword is not serialized during serialization process.This variable will be initialized with default value during deserialization. (e.g: for objects it is null, for int it is 0).
In case of static Variables:- A variable defined with static keyword is not serialized during serialization process.This variable will be loaded with current value defined in the class during deserialization.

 

transient keyword를 사용하는 이유는 serialization 과정에서 bytestream 단위로 변화시키는 것을 막기 위함이다. bytestream으로 변환이 안되면 파일에 저장이 안되게 할 수 있기 때문이다. 주로 정보 보안을 위해서 transient를 사용한다. 이 키워드를 사용하면 JVM에게 해당 변수는 persistent Java variable이 아니라는 것을 명시해준다.

 

+ Recent posts