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).]