Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

transient and volatile variables

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Kathy sierra SCJP 1.5 book i have read that transient and volatile modifiers apply only for instance variables.

Might be they are irrelevant in the following sense as I think:
1)transient ->during serialization only the object state is flattened and the class variables(I mean static varibles) wont come into picture here.
2)volatile->is meaningful only for instance varibles and not static variables, as only the objects state is being protected from any simultaneous modification.

Correct me if I am wrong.

I also compiled and ran the below program using static-transient, static-volatile combination of variables. it is perfectly compiling and running fine.

class A{
static volatile int x;
static transient int y;

public static void main(String[] args){
A a = new A();
System.out.println(x);
System.out.println(a.x);
System.out.println(y);
System.out.println(a.y);
}
}

Can any body please clarify my doubt?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of uses of modifiers that do not cause compiler errors or runtime errors which are nevertheless totally meaningless - not applicable. So, what is your doubt? Did you expect the compiler to object?

Bill
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see why volatile wouldn't be useful for class variables.

I don't have the book, but perhaps what was meant is that those modifiers don't apply to local variables?
 
Satish Ray
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My doubt is whether we can use transient and volatile variables respectively along with static modifier legally? or is it that there is no necessity for using transient and volatile modifiers along with static modifier?
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The volatile modifier is perfectly legal for both instance and static variables -- as what it does, is specify how it should behave.

The volatile modifier doesn't make sense for local variables because local variables are not accessable by multiple threads.

The transient modifier doesn't make sense for static variables, because class variables are not serialized.

Henry
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic