Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

volatile... is this not correct way?

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please go throgh my code:



Output: 10

Please clarify me about 'volatile'...
Is my comments correct...
Why output came 10 rather than expected 20.

Waiting....for your comments
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because value of member variables are decided at compile time ( not overrided ),
even if you remove the volatile key word you should get the same output.
[ July 15, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i think it has nothing to do with volatile. accessing instance variable is not like accessing instance methods.so no dynamic binding in here
a.i gets the variable from the class of reference variable a not of the object actually held in it.
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Srinivasa and Narendhra for your quick reply....

you are all mean..."volatile only effects in Thread concept...I mean the variable which participate in thread..only has volatile effect...?

Is it possible to give an example code for volatile without using thread concept...? :roll:
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am waiting for your comments... All you mean
"There is no example code for volatile without using thread concept"...

let me know please...
[ July 16, 2005: Message edited by: Ramakrishna Nalla ]
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel bad with this type of response...
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramakrishna Nalla:
Thanks Srinivasa and Narendhra for your quick reply....

you are all mean..."volatile only effects in Thread concept...I mean the variable which participate in thread..only has volatile effect...?

Is it possible to give an example code for volatile without using thread concept...? :roll:



As you mentioned there is no effect of volatile if you are not running multiple threads. So I am not sure what example you are looking for?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just feel, that the problem with the above code is that the subclass redeclares the variable already declared by superclass. The Runtime switching happens fine, If we remove it.
Please correct me if i am wrong.

Thanks,
Gokul.

Code:

Working fine:
class A{
int i=30;
}

class B extends A{
int j;
}

class Test extends B{
public static void main(String args[]){
A a;
B b1 = new B();
b1.i = 10;
a=b1;
System.out.println(a.i);
B b2 = new B();
b2.i = 20;
a=b2;
System.out.println(a.i);
}
}

Not Working Fine:

class A{
int i=30;
}

class B extends A{
int i;
int j;
}

class Test extends B{
public static void main(String args[]){
A a;
B b1 = new B();
b1.i = 10;
a=b1;
System.out.println(a.i);
B b2 = new B();
b2.i = 20;
a=b2;
System.out.println(a.i);//at compile time=a.i and at runtime b.i
}
}

Thanks,
Gokul.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't believe this has anything to do with the keyword volatile. It is due to the "true" type of a class reference.

Example:

public class A {
int i = 10;
}

public class B extends A {
int i = 99;
}

public class C {
public static void main(String[] args) {
A a = new B();
System.out.println("a.i = " + a.i); // "a.i = 10"
System.out.println("a.i = " + ((B)a).i); // "a.i = 99"

}
}

The variable 'a' true type is "A", even though the reference has been set to B. If you cast the a variable to B, then you can get at the variables in b.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic