Can we serialize the static variable?

Showing Answers 1 - 25 of 25 Answers

selwee

  • Apr 2nd, 2006
 

Yes we can serialize the static variable.if u don't want to serialize, u need to declare the variable as transient

  Was this answer useful?  Yes

Mohana

  • Apr 7th, 2006
 

I hope static/transient variables are not serializable

  Was this answer useful?  Yes

Manjunath N

  • Apr 14th, 2006
 

No, we can serialize static , transient, final variables and methods or variables in the final classes.

  Was this answer useful?  Yes

santukt

  • Apr 15th, 2006
 

By default static variables are not serializable.This is due to the reason that they are not bound to a particular object.If u want to exclude an ordinary variable from serialization then qualify that variable as transient

  Was this answer useful?  Yes

vasanta

  • May 8th, 2006
 

Plz stop to give the stupid answers If u know perfect answers then u can post.Plz dont confuse others by seeing those

  Was this answer useful?  Yes

Rajnish Kumar

  • May 13th, 2006
 

no we cant serialize the static variable

  Was this answer useful?  Yes

srinivas

  • May 21st, 2006
 

Hi
This is possible by providing the following two methods in our class
as shown below. writeObject(),readObject().
import java.io.*;

public class Ab

{
 
public static void main(String a[])
  
throws Exception
 
{
  
FileOutputStream fos = new FileOutputStream("data.txt");
  
ObjectOutputStream oos = new ObjectOutputStream(fos);
  
Abc1 a1 = new Abc1();
       
a1.i = 25;
  
a1.j = 35;
  
a1.k = "srinivas";
oos.writeObject(a1);
  
fos.close();
 
}

}
import java.io.*;

public class Abc1 implements Serializable

{
 public int i;
 
public static float j;
 
public  static String k;

private void writeObject(java.io.ObjectOutputStream out)
    
throws IOException
 
{
   
System.out.println("writeObject");
 
out.writeInt(i);
 out.writeFloat(j);
   
}
 
private void readObject(java.io.ObjectInputStream in)
    
throws IOException, ClassNotFoundException
   
{
    
System.out.println("readObject");
 
i= in.readInt();
  
j= in.readFloat();
   
}

}
ok bye...................
keeps smiling and mailing
if any want java material & doubts regarding j2se ,j2ee plz send mail to
bora_srinivasarao@yahoo.co.in
Note:If possible I will clarify doubts.but I send java material.

  Was this answer useful?  Yes

Abhijit

  • May 24th, 2006
 

Yes .refer Thinking in java Bruce Eckel Page No:633 Version-2 for detailed example on serializing static variables

  Was this answer useful?  Yes

radhika

  • Jun 14th, 2006
 

Several techniques are available to protect sensitive data in classes. The easiest is to mark fields that contain sensitive data as private transient. transient and static fields are not serialized or deserialized. Marking the field will prevent the state from appearing in the stream and from being restored during deserialization. Since writing and reading (of private fields) cannot be superseded outside of the class, the class's transient fields are safe.

  Was this answer useful?  Yes

tarunarora

  • Aug 24th, 2006
 

These two links will clear all your doubts: -http://www.onjava.com/pub/a/onjava/excerpt/JavaRMI_10/index.html?page=3http://www.allapplabs.com/interview_questions/java_interview_questions_3.htm#q17

  Was this answer useful?  Yes

kiran kumar k

  • Oct 9th, 2006
 

which one we have to confirm if one guy is sending means tell i know 100%like that

  Was this answer useful?  Yes

gopikrishna

  • Nov 2nd, 2006
 

 u r right vasantha... plz give the correct answer.

  Was this answer useful?  Yes

sampra

  • Feb 14th, 2008
 

Yes we can serialize the static variable as i have checked on system but theory is telling we cant ..can anyone make it clear

  Was this answer useful?  Yes

Ashish

  • May 21st, 2012
 

Static variable can not be serialized.... please follow find below example.

Test3==>

Code
  1. span style="color: #006699;">a.b.c.d.ejava.io.FileInputStreamjava.io.FileOutputStreamjava.io.IOExceptionjava.io.ObjectInputStreamjava.io.ObjectOutputStreama.b.c.d.Test2/**

  2.          * @param args

  3.          */// TODO Auto-generated method stub

  4. "Test2");

  5.         t.setTestVar("Ashish"// TODO Auto-generated catch block

  6. "Test3""==="// TODO Auto-generated catch block

  7. // TODO Auto-generated catch block

  8. /**

  9.             * This is the default implementation of writeObject.

  10.             * Customise if necessary.

  11.             */"D:/Disk Usage/Expenses/t.ser""D:/Disk Usage/Expenses/t.ser"a.b.c.djava.io.Serializable"Test"

  Was this answer useful?  Yes

Ravi

  • Dec 1st, 2014
 

You are saying that transient and static fields are not serialized. For every variable which does not want to participate in serialization then we can declare it as static right. What is the use of transient then?

  Was this answer useful?  Yes

Vipul Tare

  • Apr 20th, 2015
 

No. The static variables belong to the class are not the part of the state of the object so they are not saved as the part of serialized object.

  Was this answer useful?  Yes

swapna

  • Jul 7th, 2015
 

Static variable is class variable but always part of each object also, So we can serialize the static variables.

Always serialized static variable holds the value of particular state object.

Code
  1. span style="color: #006699;">java.io.FileInputStreamjava.io.FileOutputStreamjava.io.ObjectInputStreamjava.io.ObjectOutputStreamjava.io.Serializable"train";

  2.    // private NonSerial nonSerial;

  3. "Serialize Test""test1.ser""test1.ser""Value :  ""Value Of Tansient ""Value Of Static ""Value of Instace ""Value of String   ""Value of Instace "

  Was this answer useful?  Yes

shanu

  • May 9th, 2016
 

You cannot serialize static variable as they belong to class not obj. and serialization is all about object.
static int x= 10; int y =5 , if you seralize both the ans will be 10 and 5.

But wait 10 is coming from class file definition not the file where you saved the value after serialization.
to furnish the prove.
Step 1: static int x =5;y=10
Step 2: serialization performed
Step 3: x= x+1
Step 4: De-serialized obj.
Step 5: x= 6 , y=10

Hence proved static variable do not participate in serialization process.

  Was this answer useful?  Yes

ashok

  • Oct 14th, 2018
 

Your example absolute good but, we cant serialize static and transient. if you want deserialize again and check it it goes to default values means that variables are new variables to deserialization class.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions