Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How this output is possible?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can anyone please explain me how the following output is possible?


Output is: 200 and 5

Thanks in advance.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because the variable 'rate' is static. It is a class variable (and not an instance variable) meaning, it is common to all the objects of that class. Any changes made to this variable from within any instance is common to all the other instances of that class.
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Krishnan
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by m ali:
Hi All,
Can anyone please explain me how the following output is possible?


Output is: 200 and 5

Thanks in advance.



HI Ali,

static in rate belongs to class Account not to any particula instance of Account. This variable is shared by all instances of Account. Modifing it's value using once instance will change it's value visible to other instances.

At line 1

Assigning rate a value of 7 changes the value of class variable rate to 7.

At line 2
Assigning class variable a value of 5 will change the value of rate back to 5.

Hope this helps.

Raghav
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raghav, got it.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see that you have initialised int rate as STATIC.

static int rate;

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#230789
If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized (�12.4).


so
even though you have created two instances of class account - they share this variable and as you have changed it's value from rate=7 to rate=5

hope that helps

cheers
iza
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic