Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why transient variable cannot be declared as final or static?

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
a transient variable cannot be declared as final or static"
What is the logic behind this?

 
a transient variable cannot be declared as final or static"
I just want to know what is the logic behind this rule...
What is not clear...?
 
a car cannot be red"
What is the logic behind this?

Where did you find that information? Your compiler? An article? JVM spec?

Cheers,
Dian
 
When I say Java rules of course I mean the JVM spec.
Try to define one and see that you can't...


 
I've done and I can.

Will you post the details about this or will you continue hiding them?

Cheers,
Dian
 
It is printed on hard copy and I don't have a link right now. Will you please post your decleration so I could try it? Which JDK you are using and which work environment?
 
Here is the quote as well:

"transient
The transient modifier is used with variables. It indicates that a variable may not be serialized. It is
typically used to reduce the possibility that an object containing sensitive data might be written to a
stream. A transient variable may not be declared as final or static.
 
Regardless of whether this is mentioned anywhere or not, I don't think it makes any sense to mark statics as transient anyway. Statics aren't part of an instance (the're static!) and so aren't relevent to an instances persisted state. I've tried it and a deserialized object containing a non-transient static does not override a subsequent change to that static value. So transient has no effect on statics. Forgetaboutit.

Finals can be serialized. A transient final is given a default value on deserialization, whereas a non-transient one re-acquires its serialized value.

Tim
 
And my expl. is :

Transient variables are not just about serialization.

Transient variables to the interpreter are not guaranteed to be of a specific bytecode structure - so in memory they cannot be relied upon. They are not part of the object instance's state (this could be perhaps because they relied on native/JNI code or something).

So this means that because you cannot (or may not) be able to define them at runtime, they cannot be final or static - because these modifiers define a specific memory footprint - but by its nature, transient data may not occupy memory (or certianly not a static or final footprint).

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
OK, ignore everything I just said ... clearly going mad.

This class compiles fine :

Code:
public class Bla {
        public static final transient int k = 0;
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I thought we declare a variable transient, when different threads have asynchronous access to it, to make sure it isn't read from cache, but evaluated every time when it is read.

That doesn't make much sense for a final variable, because after initialisation it can't be changed, and therefore reading it from a cache should be legal.

however this:
Code:
 public class Transient
{
	transient int a;
	transient static int b;
	transient final int c;
	
	/** */
	public Transient (String param)
	{
		c = 17;
		b = 9;
		a = 4;
	}
}
compiles and runs flawlessly.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top