BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

variables

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> JBuilder Java
View previous topic :: View next topic  
Author Message
Jules
Guest





PostPosted: Sun Sep 07, 2003 2:33 pm    Post subject: variables Reply with quote



Quickie from a newbie
How do I change the value of a variable, say var, of ClassA from a different
class, say ClassB?
TIA
Remove XX if replying direct


Back to top
Gordon Beaton
Guest





PostPosted: Sun Sep 07, 2003 2:47 pm    Post subject: Re: variables Reply with quote



[ comp.lang.java removed ]

On Sun, 7 Sep 2003 15:33:31 +0100, Jules wrote:
Quote:
How do I change the value of a variable, say var, of ClassA from a
different class, say ClassB?

Ideally, variables in other classes should be read or modified only
through accessor methods in that class.

However you can specify a variable in another class the exact same way
you specify a method in another class: using the dot (.) operator. The
same access rules apply.

If the variable is declared static, qualify it with its classname:

ClassA.foo = ...

If it isn't declared static, you need to use an instance of that
class:

ClassA a = new ClassA();

a.foo = ...

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e

Back to top
Ryan Stewart
Guest





PostPosted: Sun Sep 07, 2003 8:18 pm    Post subject: Re: variables Reply with quote



"Jules" <jb (AT) burnsvilleXX (DOT) freeserve.co.uk> wrote

Quote:
Quickie from a newbie
How do I change the value of a variable, say var, of ClassA from a
different
class, say ClassB?
TIA
Remove XX if replying direct

If I understand your question, you don't unless var's type is some

super-class of both ClassA and ClassB. If this is what you want to do:

ClassA var;
var = new ClassA();
// Do something with var
var = new ClassB();

That doesn't work. It's like saying:

int x = "Hello world.";

Some languages would call it a type mismatch. You could, however, say:

Object var;
var = new ClassA();
// Do something with var
var = new ClassB();



Back to top
Brad BARCLAY
Guest





PostPosted: Mon Sep 08, 2003 2:19 am    Post subject: Re: variables Reply with quote

Jules wrote:
Quote:
Quickie from a newbie
How do I change the value of a variable, say var, of ClassA from a different
class, say ClassB?

Quick question, but a long, convoluted answer Smile.

The answer is "it depends". There are three factors that need to be
dealt with:

1) Is the variable a static (or class) variable, or is it an instance
(object) variable?

2) What is the variable's visibility?

3) Are ClassA and ClassB in the same package?

(There are potentially more than three if you're working in a
multithreaded environment, but as you said you're a newbie, I'll presume
not).

Look up "member visibility" in any convienent Java programming
reference. You first need to be certain that ClassB has the necessary
rights to modify a field (variable) in ClassA in the first place.

Once you've assured yourself that you have such rights, how you'll do
the assignment depends on wether or not the variable is static.

If the variable is static, use:

ClassA.var = somevalue;

If the variable is non-static, use:

instanceOfClassA.var = somevalue;

...where "instanceOfClassA" is a field that was generated by creating a
"new" instance of ClassA, ie:

ClassA instanceOfClassA = new ClassA();

HTH!

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> JBuilder Java All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.