aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why a transient variable can be declared with static modifier?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why a transient variable can be declared with static modifier??" Watch "why a transient variable can be declared with static modifier??" New topic
Author

why a transient variable can be declared with static modifier??

Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68



declaring a variable transient means that it can't be serialized and static variables are exempted from serialization because serialization is only for instance variables.

I thing,there is no use to declare them together.

There is no relationship between these two variables.if a variable is static than it can't be serialized so,why java allows static with transient???
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

well, but static/class vaiable is not an instance variable right?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Sumit Khurana wrote:

declaring a variable transient means that it can't be serialized and static variables are exempted from serialization because serialization is only for instance variables.

I thing,there is no use to declare them together.

There is no relationship between these two variables.if a variable is static than it can't be serialized so,why java allows static with transient???


Saying something is static and transient is redundant. Just like saying a variable in an interface is public static final.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Yes, I think the original poster understands that, but he's wondering, why did Java allow it?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Stephan van Hulst wrote:Yes, I think the original poster understands that, but he's wondering, why did Java allow it?


I wonder if anyone knows why. I for one, am aware that redundancy is sprinkled into the language here and there.

This question probably falls into the same category as 'Why should public classes have the same name as the file in which they reside ?'. Its just the way it is I guess.

Turning the question on its head, how should the compiler handle redundancy ? Throw a compile time error ?
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

I know that static variables can't be serialized and still we can add the transient modifier with it. There is no use of it. it might be redundant. I am just wondering that java allows it. so,there might be some purpose behind this.

Yes, I think the original poster understands that, but he's wondering, why did Java allow it?

right stephan.......

Turning the question on its head, how should the compiler handle redundancy ? Throw a compile time error ?

not in every case. but might be in this case because you are saying that if serialization occurs than leave this one.but this variable is already out of serialization.
Saying something is static and transient is redundant. Just like saying a variable in an interface is public static final.



but bala i think,declaring a variable public static final in an interface is not reduntant.
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

a
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

It is redundant, because all variables in an interface are public, static, and final.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

I know that static variables can't be serialized and still we can add the transient modifier with it. There is no use of it. it might be redundant. I am just wondering that java allows it. so,there might be some purpose behind this.


Yes I missed the purpose of that question. Stephan caught it.

not in every case. but might be in this case because you are saying that if serialization occurs than leave this one.but this variable is already out of serialization.


A compile time error is a little harsh, no matter what the case is. Redundancy is confusing, but not necessarily a scenario that leads to an 'error'. A warning might suffice.
Sven Mathijssen
Greenhorn

Joined: Aug 29, 2010
Posts: 19
Sumit Khurana wrote:

declaring a variable transient means that it can't be serialized and static variables are exempted from serialization because serialization is only for instance variables.

I thing,there is no use to declare them together.

There is no relationship between these two variables.if a variable is static than it can't be serialized so,why java allows static with transient???


You are right, but I think the reason the Java developers chose not to raise an error or even a warning with this is that it introduces unnecessary compile-time checks. Why not allow it if it is perfectly legal code? The fact that it is redundant does not mean it is wrong.
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

Thanks ranchers now i understood ......it is like declaring a private and final together....

adding static to a variable means you don't want to serialize it....and declaring a variable transient also means the same...but earlier i am thinking in a different way...
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Sumit Khurana wrote:Thanks ranchers now i understood ......it is like declaring a private and final together....

adding static to a variable means you don't want to serialize it....and declaring a variable transient also means the same...but earlier i am thinking in a different way...


private and final are not the same things.

A variable becoming transient because it is static is a side effect. The intent is for a variable to be static. The side effect is the transiency that comes with the decision to make it static
shivani vidit
Greenhorn

Joined: Oct 11, 2007
Posts: 14
Hi Sumit,

You are right that at making a transient variable static is redundncy , but this is redudndacy only if you think from serialization prespective,,, your question is why java allows to do so?... that's bacuse making a variable static has other significance as well apart from just not being seralized..by making it static assures that you can use it without craeting the insatnce of the class .
so if variable is only trainsient : it means it is a non serializable insatnace variable
static transient means: its is non serializable class variable..
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

Thanks ranchers now i understood ......it is like declaring a private and final together....

adding static to a variable means you don't want to serialize it....and declaring a variable transient also means the same...but earlier i am thinking in a different way...


In this post, i wrote only from my serialization perspective...and i know what is the role of static...

so if variable is only trainsient : it means it is a non serializable insatnace variable
static transient means: its is non serializable class variable..


only static means also the same as of declaring it with static and transient :as you wrote non serializable class/static variable

private and final are not the same things.


but Deepak just see it from overriding part...both are telling you the same thing...that you can't override the private methods because they are not visible and you can't override the final methods because they can't be change....means declaring a private method final is also redundant....

both of them have different work....but i am just taking an example which has a similar case...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why a transient variable can be declared with static modifier??