 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jules Guest
|
Posted: Sun Sep 07, 2003 2:33 pm Post subject: variables |
|
|
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
|
Posted: Sun Sep 07, 2003 2:47 pm Post subject: Re: variables |
|
|
[ 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
|
Posted: Sun Sep 07, 2003 8:18 pm Post subject: Re: variables |
|
|
"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
|
Posted: Mon Sep 08, 2003 2:19 am Post subject: Re: variables |
|
|
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 .
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 |
|
 |
|
|
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
|
|