>>> How can I have two windows share the same data?
>>
[quoted text clipped - 3 lines]
> Yes, I am new to Java, although I have been programming for many
> years. I was looking for more of a "push the green button" answer.
Sure, but I thought you might also appreciate the "dang, if only I'd
been told about the red and blue buttons three months ago" answer.
> Normally, in other languages, when one wants to share data inside a
> program, a global variable is used.
True, but often global variables are regarded as a very bad thing (tm).
Of course, like many languages, Java also provides you with the means to
shoot yourself in the foot. I'll assume you know all this and are a
skilled Colt 45 gunslinger picking up a loaded Glock 9mm for the first
time (and that I'm out of range).
> Does Java have a global variable
> that can be seen by any classes called by the class where the
> variable is defined?
One way ^H^H^H^H^H^H^H^H The green button is ...
class Global {
public static Bullet myBullet;
}
class Gun {
Gun() {
myBullet = new Bullet("OldPro");
new Foot().shoot();
}
}
class Foot {
private JTextField flesh;
public void shoot() {
flesh.setText(Global.myBullet);
}
}
class Bullet extends String {}
I learned that using the green button was usually a bad choice. Maybe
I'm wrong, you can judge for yourself.
> Or do I have to create a listener for each and
> every variable that needs to be viewable between classes?
Definitely not.
I'd probably be passing needed instance variables as arguments to
methods that, er, need them. Often you can bundle up a whole bunch of
variables as instance variables in a single instance of some container
class.
Good luck! :-)
P.S.
You might like to search Google Groups for prior discussion of "Global
Variables" in comp.lang.java.programming.
RedGrittyBrick - 11 Dec 2007 17:47 GMT
> class Gun {
> Gun() {
> myBullet = new Bullet("OldPro");
Global.myBullet = new Bullet("OldPro");
^^^^^^^
> new Foot().shoot();
> }
> }
I suppose I should test stuff before posting.
OldPro - 11 Dec 2007 19:51 GMT
On Dec 11, 11:47 am, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
wrote:
> > class Gun {
> > Gun() {
[quoted text clipped - 8 lines]
>
> I suppose I should test stuff before posting.
Thank you for your help!