This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Visibility of changes to an object's variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Visibility of changes to an object Watch "Visibility of changes to an object New topic

Visibility of changes to an object's variables?

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
an explanation for a question at examulator had this quote - "If an object is passed to a method, changes to its variables will be valid wherever the class is visible." ... but there are situations aren't there, where a change is not visible? if the object is passed to the method, but the change is made to the variable not via the "." way of refering to it?
so in the following code snippet, in the deduct() method, the reference to balance is via a.balance .... and the change is visible later, but if the reference to balance variable was just "balance" and not "a.balance", the change would NOT be visible outside the deduct() method?
public void Go(){
Account personal = new Account();
Account business = new Account();
business.rate =5;
public void deduct(Account a){
class Account{
static int rate;
public int balance=100;

giddee up
El Roy Cabildo

Joined: Oct 19, 2002
Posts: 23
yes of course you cannot directy get the value "balance" without creating the object first(Account) because its a member variable. But with "rate" you can call it with or without creating the object because it is static variable...chaging it like personal.rate = 7 and business.rate =5. The variable "rate" is shared by both business and personal.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
thanks for replying, but what i am asking is not about whether a new object has to be created to be sent to the deduct() method.
i am asking is it possible for deduct() method to be passed an Account object, and do things with the balance variable just by referencing the balance variable, and not a.balance? And then not have these changes visible outside the deduct() method?
a bit confusing, maybe i am going about asking these things a bit confusingly.
I agree. Here's the link:
subject: Visibility of changes to an object's variables?
jQuery in Action, 3rd edition