aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can transient variables be static?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can transient variables be static??" Watch "Can transient variables be static??" New topic
Author

Can transient variables be static??

Shameen MK
Greenhorn

Joined: Sep 28, 2000
Posts: 22
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

Joined: Oct 06, 2000
Posts: 22
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

Joined: Oct 11, 2000
Posts: 5

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.

 
Consider Paul's rocket mass heater.
 
subject: Can transient variables be static??