Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can transient variables be static??

 
Shameen MK
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plz tell me if transient variables can be static?? The book by Rasmussen says that these cannot b static, at the same time in the questions it does not mark a :
final transient static private double PI= 3.14...........
to b illegal. I tried to run it and it works, then why does the book at pg 126 says that "transient modifier cannot be specified for static variables" ??
 
Jon Aryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,
final static transient String s = "Me"
is a valid declaration .
--------------------------------------------
import java.io.*;
class A implements Serializable {
final static transient String s = "Me"; // Serialized [ transient !!, but static ]
transient String ss = "Marlboro"; // Not serialized
static String sss = "Lights";
static B b = new B();
// By default serializable fields of a class are defined to be the
// non-transient and non-static fields. Eh!. V will catch him later !!.
// Spare him 4 now.
int i;
A() {
}
}
class Tester {
public static void main(String args[]) throws IOException, ClassNotFoundException {
A a = new A();
FileOutputStream out = new FileOutputStream("tmp.ser");
ObjectOutputStream o = new ObjectOutputStream(out);
o.writeObject(a);
FileInputStream in = new FileInputStream("tmp.ser");
ObjectInputStream o2 = new ObjectInputStream(in);
A aa = (A) o2.readObject();
System.out.println("Static Transient string !! --> " + aa.s);
System.out.println("Instance variable i --> " + aa.i);
System.out.println("Non-static Transient string --> " + aa.ss);
System.out.println("Static string --> " + aa.sss);
System.out.println("Instance variable of class B --> " + aa.b.i);
}
}
class B implements Serializable {
int i = 20;
}

-------------------
Outout is
* Static Transient string !! --> Me
* Instance variable i --> 0
* Non-static Transient string --> null
* Static string --> Lights
* Instance variable of class B --> 20
-------------------

Originally posted by Shameen Mazhar:
Plz tell me if transient variables can be static?? The book by Rasmussen says that these cannot b static, at the same time in the questions it does not mark a :
final transient static private double PI= 3.14...........
to b illegal. I tried to run it and it works, then why does the book at pg 126 says that "transient modifier cannot be specified for static variables" ??


[This message has been edited by Jon Aryan (edited October 08, 2000).]
 
Velmurugan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nice example. But the truth is, static variables are NOT serialized anyway. Marking them transient doesn't do anything. Compiler allows the declaration, since there's no harm.
See the two lines of code added. It creates a new object and changes the static variable through it AFTER the object in question has been read from the stream. The println statement prints the new value of the static variable. This explains that static variables are not saved as part of the object's state.
The println statements print the static variable values, since variables are resolved at compile time based on the reference type, not from the object read from the stream.
Hope this helps.
-Velmurugan.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic