GeekInterview.com
Series: Subject: Topic:
Question: 365 of 573

Can we serialize the static variable?

Asked by: Interview Candidate | Asked on: Apr 2nd, 2006
Showing Answers 1 - 20 of 20 Answers
selwee

Answered On : 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

  
Login to rate this answer.

We cannot serialize static and transient variables. We serialize objects to store them so they can retrieved later for any use.

  
Login to rate this answer.

yes we can serialize this but how to do this which time i required this

  
Login to rate this answer.
Mohana

Answered On : Apr 7th, 2006

I hope static/transient variables are not serializable

  
Login to rate this answer.
Manjunath N

Answered On : Apr 14th, 2006

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

  
Login to rate this answer.
santukt

Answered On : Apr 15th, 2006

View all answers by santukt

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

  
Login to rate this answer.
vasanta

Answered On : 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

  
Login to rate this answer.
Madhavikari

Answered On : May 8th, 2006

View all answers by Madhavikari

No,

we cannot.

We cannot serialize the static and transient variables.

Yes  2 Users have rated as useful.
  
Login to rate this answer.

Please go to this link and read about serializationhttp://java.sun.com/docs/books/tutorial/essential/io/providing.html.It's should clear everything

  
Login to rate this answer.
Rajnish Kumar

Answered On : May 13th, 2006

no we cant serialize the static variable

  
Login to rate this answer.
srinivas

Answered On : 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.

  
Login to rate this answer.
Abhijit

Answered On : May 24th, 2006

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

  
Login to rate this answer.
radhika

Answered On : 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.

  
Login to rate this answer.
tarunarora

Answered On : Aug 24th, 2006

View all answers by tarunarora

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

  
Login to rate this answer.
kiran kumar k

Answered On : Oct 9th, 2006

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

  
Login to rate this answer.
gopikrishna

Answered On : Nov 2nd, 2006

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

  
Login to rate this answer.

No.we can't initialize the static variable.

  
Login to rate this answer.
sampra

Answered On : Feb 14th, 2008

View all answers by sampra

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

  
Login to rate this answer.
vinaymudgil007

Answered On : May 19th, 2008

View all answers by vinaymudgil007

No static variables can't be serialzed......

any doubts, let me know i can give an example???

  
Login to rate this answer.
Ashish

Answered On : 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.  

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.