• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can transient variables be static??

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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" ??
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic