• 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

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

 
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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???
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, but static/class vaiable is not an instance variable right?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think the original poster understands that, but he's wondering, why did Java allow it?
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is redundant, because all variables in an interface are public, static, and final.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic