rynop All American 829 Posts user info edit post |
I'm new to java - what does protected transient mean? I'm having trouble finding documentation on it.
public class TheBean extends Container { private transient OfficeConnection mConnection; ... 11/3/2005 4:30:50 PM |
xienze All American 7341 Posts user info edit post |
Err, did you mean private or what?
Protected access in Java means the field or method is accessible to the class, its subclasses, and all the other classes in the same package. The transient keyword indicates that the field isn't part of the data saved when the class is serialized. 11/3/2005 8:03:09 PM |
dFshadow All American 9507 Posts user info edit post |
http://java.sun.com/docs/books/tutorial/essential/io/providing.html http://mindprod.com/jgloss/transient.html 11/4/2005 12:02:18 AM |
madmoose Starting Lineup 68 Posts user info edit post |
Quote : | "Protected access in Java means the field or method is accessible to the class, its subclasses, and all the other classes in the same package." |
If you don't specify public, private, or protected, the class/field is only accessible from within the same package.
transient means the field won't get serialized if you are doing any serialization of your classes.
[Edited on November 4, 2005 at 5:02 PM. Reason : messed up quote.. I suck at life.]11/4/2005 5:02:15 PM |
xienze All American 7341 Posts user info edit post |
Sorry, you're wrong. Protected access is also accessible to all classes in the same package. I'll leave the proof as an exercise to the reader. 11/4/2005 6:31:36 PM |
psnarula All American 1540 Posts user info edit post |
okay i've wondered about this for a long time: what is Serializable and when do we want a class to implement this interface? 11/4/2005 7:32:48 PM |
scud All American 10804 Posts user info edit post |
Serialize is another way to say 'marshall'.
Basically you write out the Class instance member data into a form that can be persisted and/or transmitted. Obviously since this persistance is heirarchical (if you have other class members) they need to be Serializable as well. 11/4/2005 7:44:33 PM |
psnarula All American 1540 Posts user info edit post |
sorry i don't follow. i dont know what marshall means either... 11/4/2005 8:02:15 PM |
scud All American 10804 Posts user info edit post |
okay...one very primitive example of serialization would be to 'marshall' a class out into XML
Say you have a Java class called Dog
public class Dog { private String _breed = "Golden Retriever"; private String _color = "Blonde"; private int _height = 22; private int _weight = 105; private String _name = ""; private transient boolean initialized = false;
public Dog(String name) { //All our dogs look the same _name = name; initialized = true; } }
Dog bogey = new Dog("Bogey");
If i serialize the object bogey into XML, it could look something like :
<Dog> <Breed value="Golden Retriever" type="String" /> <Name value="Bogey" type="String" /> <Color value="Blonde" type="String" /> <Height value=22 type="int" /> <Weight value=105 type="int" /> </Dog>
This is a pretty simplistic example but it should convey the point. Now another process or thread can read the serialized string and reconstruct or 'unmarshall' the object into a new copy. Notice that 'initialized' isn't serialized into the XML because it was marked 'transient' . We're saying we don't care to perisist that member usually because it's irrelevant and saves space, code and or bandwidth. In this case it's obvious we don't need the initialized member since it doesn't make sense to serialize an uninitialized object.11/4/2005 8:33:43 PM |
psnarula All American 1540 Posts user info edit post |
thanks. makes more sense now. 11/4/2005 9:01:56 PM |
madmoose Starting Lineup 68 Posts user info edit post |
Quote : | "Sorry, you're wrong. Protected access is also accessible to all classes in the same package. I'll leave the proof as an exercise to the reader." |
I stand corrected. Though I can't recall seeing a Class that ever used the protected keyword when it wasn't intended to be subclassed. Besides, they aren't really only accessible from the same package since you can subclass and gain access to fields/methods from a seperate package. Just seems like a bad practice imho.11/4/2005 11:10:16 PM |
xienze All American 7341 Posts user info edit post |
Quote : | "Just seems like a bad practice imho." |
It has to do with the philosophy behind Java's access modifiers. From most restrictive to least restrictive are private, (default), protected, and public. For protected not to allow access to other classes in the same package would make protected more restrictive than (default).11/5/2005 10:34:20 AM |
rynop All American 829 Posts user info edit post |
^^^^thanks scud. That example helped alot. 11/6/2005 3:54:57 PM |
boonedocks All American 5550 Posts user info edit post |
As someone who knows nothing about CS, this thread's title made me chuckle. 11/6/2005 4:42:03 PM |
philihp All American 8349 Posts user info edit post |
think of serializing as like "freeze drying" a java object.
you usually don't need to worry about transient member variables, since you usually aren't making classes that implement Serializable (although you might occationally extend a class that implements it... like Swing classes for example). 11/6/2005 8:34:24 PM |