Can we serialize the static members?

Showing Answers 1 - 8 of 8 Answers

RAHUL

  • Apr 6th, 2006
 

no

  Was this answer useful?  Yes

prabhakar

  • Apr 6th, 2006
 

Yes, We can serialize the Static data also.For this we use the Externalizable tagged interface anduse writeObject() is used.Otherwise if we implement the Serializable interface if we serialize the data then static variable value is stored as zero.So instead of this we use Externalizable interface.

  Was this answer useful?  Yes

srinivas

  • May 17th, 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 baye...................

keeps smiling and mailing

bora_srinivasarao@yahoo.co.in

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

james

  • Sep 17th, 2006
 

srinivas what are you doing?you trying to deviate the programmer by your own code.static members are cant be serializablewhat ever you did in side your program that is just using the local heap of the jvm so the values of the static variables are come outif you close the streams explicitly then you cant get the same variables ok.try thisimport java.io.*;public class st implements Serializable{transient public int i; public static float j; public String k; public st(int a,float b,String c){i=a;j=b;k=c;} public st(){}}import java.io.*;public class sto{ public static void main(String a[]) throws Exception {File f=new File("datajames.txt"); FileOutputStream fos = new FileOutputStream(f); ObjectOutputStream oos = new ObjectOutputStream(fos); st a1 = new st(); a1.i = 25; a1.j = 35; a1.k = "srinivas"; oos.writeObject(a1); fos.close(); FileInputStream fos1 = new FileInputStream(f); ObjectInputStream oos1 = new ObjectInputStream(fos1); st nn=(st)oos1.readObject(); System.out.println("the result is"+nn.k+" "+nn.i+" "+nn.j); }}import java.io.*;public class stor{ public static void main(String a[]) throws Exception {File f=new File("datajames.txt"); FileInputStream fos1 = new FileInputStream(f); ObjectInputStream oos1 = new ObjectInputStream(fos1); st nn=(st)oos1.readObject(); System.out.println("the result is"+nn.k+" "+nn.i+" "+nn.j); fos1.close(); oos1.close(); }}

  Was this answer useful?  Yes

Tafazzul hasan

  • Feb 21st, 2007
 


We can not Serialize Static members.

  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