Can we serialize the static variable?

Showing Answers 1 - 43 of 43 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.  

  2.  

  3. package a.b.c.d.e;

  4.  

  5. import java.io.FileInputStream;

  6. import java.io.FileOutputStream;

  7. import java.io.IOException;

  8. import java.io.ObjectInputStream;

  9. import java.io.ObjectOutputStream;

  10.  

  11. import a.b.c.d.Test2;

  12.  

  13. public class Test3 {

  14.  

  15.         /**

  16.          * @param args

  17.          */

  18.         public static void main(String[] args) {

  19.                 // TODO Auto-generated method stub

  20.         Test2 t= new Test2();

  21.         StringBuffer sb = new StringBuffer();

  22.         Test2.setVar("Test2");

  23.         t.setTestVar("Ashish");

  24.         Test3 t3 = new Test3();

  25.         try {

  26.                         t3.writeObject(t);

  27.                 } catch (IOException e) {

  28.                         // TODO Auto-generated catch block

  29.                         e.printStackTrace();

  30.                 }

  31.         Test2.setVar("Test3");

  32.         try {

  33.                 t = t3.readObject();

  34.                         System.out.println(t.getTestVar()+"==="+t.getVar());

  35.                 } catch (IOException e) {

  36.                         // TODO Auto-generated catch block

  37.                         e.printStackTrace();

  38.                 } catch (ClassNotFoundException e) {

  39.                         // TODO Auto-generated catch block

  40.                         e.printStackTrace();

  41.                 }

  42.        

  43.         }

  44.          

  45.             /**

  46.             * This is the default implementation of writeObject.

  47.             * Customise if necessary.

  48.             */

  49.             private void writeObject(Test2 t) throws IOException {

  50.                 FileOutputStream f = new FileOutputStream("D:/Disk Usage/Expenses/t.ser");

  51.                 ObjectOutputStream out = new ObjectOutputStream(f);

  52.                 out.writeObject(t);

  53.                 out.close();           

  54.             }

  55.             private Test2 readObject() throws IOException, ClassNotFoundException {

  56.                 FileInputStream f = new FileInputStream("D:/Disk Usage/Expenses/t.ser");

  57.                 ObjectInputStream out = new ObjectInputStream(f);

  58.                 Test2 t = (Test2)out.readObject();

  59.                 out.close();   

  60.                 return t;

  61.             }

  62. }

  63.  

  64.  

  65. Test2=====>

  66. package a.b.c.d;

  67.  

  68. import java.io.Serializable;

  69.  

  70. public class Test2 implements Serializable{

  71.         private static String var = "Test";

  72.         private String testVar;

  73.  

  74.         public String getTestVar() {

  75.                 return testVar;

  76.         }

  77.  

  78.         public void setTestVar(String testVar) {

  79.                 this.testVar = testVar;

  80.         }

  81.  

  82.         public static String getVar() {

  83.                 return var;

  84.         }

  85.  

  86.         public static void setVar(String var) {

  87.                 Test2.var = var;

  88.         }

  89. }

  90.  

  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.  

  2.  

  3. import java.io.FileInputStream;

  4. import java.io.FileOutputStream;

  5. import java.io.ObjectInputStream;

  6. import java.io.ObjectOutputStream;

  7. import java.io.Serializable;

  8.  

  9. public class SerializeStaticVarExp implements Serializable {

  10.     private static final long serialVersionUID = 1L;

  11.     static int staticVar=56;

  12.     transient int transientVar=78;

  13.     int instanceVar=0;

  14.     int binstanceVar1=2;

  15.     String cstringVar="train";

  16.    // private NonSerial nonSerial;

  17.     SerializeStaticVarExp(int a, int s,String n){

  18.       this.instanceVar=a;

  19.       this.binstanceVar1=s;

  20.       this.cstringVar=n;

  21.       this.transientVar=a;

  22.     }

  23.    

  24.     public static void test(){

  25.        

  26.         staticVar=88;

  27.     }

  28.     public static void main(String [] args)  {

  29.  

  30.         test();

  31.       SerializeStaticVarExp c = new SerializeStaticVarExp(12,45,"Serialize Test");

  32.       try {

  33.        FileOutputStream fs = new FileOutputStream("test1.ser");

  34.        ObjectOutputStream os = new ObjectOutputStream(fs);

  35.        os.writeObject(c);

  36.        os.close();

  37.       } catch (Exception e) {  e.printStackTrace(); }

  38.       try {

  39.         FileInputStream fis = new FileInputStream("test1.ser");

  40.         ObjectInputStream ois = new ObjectInputStream(fis);

  41.         SerializeStaticVarExp c1 = (SerializeStaticVarExp)  ois.readObject();

  42.         System.out.println("Value :  "+c1.binstanceVar1);

  43.         System.out.println("Value Of Tansient "+c1.transientVar);

  44.         System.out.println("Value Of Static "+c1.staticVar);

  45.         System.out.println("Value of Instace "+c1.instanceVar);

  46.         System.out.println("Value of String   "+c1.cstringVar);          

  47.         System.out.println("Value of Instace "+c1.binstanceVar1);

  48.         System.out.println(c1.serialVersionUID);

  49.        

  50.         ois.close();

  51.       } catch (Exception e) {

  52.         e.printStackTrace();

  53.       }

  54.      }

  55.     }

  56.  

  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