Sharing variables between GUI elements in Java

Status
Not open for further replies.

Chrisblue

Ars Legatus Legionis
12,154
What would be the best way to share variables/objects between different GUI elements in java? Basically I have several ArrayList objects I want my modal dialogs to have access to. Also, where is the best place to create such shared variables? In the application class, or in the main View class that it spawns?

I was thinking of just overriding the dialog's constructor to pass references to the needed variables or maybe putting in a function to pass the references after creation. However if there is such a thing as session variables or application-level variables I could use that would be convenient.

To note, I am looking for simplicity at the moment. Speed/memory is not a concern for this little app of mine.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
You don't share variables[1], you share instances of objects. This means for example that you cannot 'share' an unboxed primitive.

Either a variable is in scope to two separate instances, perhaps a private static field in the class (which implies the class itself has ownership of it) or a public static, in which case the variable is global in scope. You cannot do anything at runtime to alter what code can access this variable except by reflection.

What you can do is pass the contents of a variable to two or more different functions/constructors which will both have their own variable containing a reference to the supplied instance. If you never change the contents of these variables after that then they all share the same instance (and thus changes to it's state from one will be visible to the others) but if you assign a new instance to any of the variables this is not visible to the others.

This is perhaps teaching you to suck eggs, but you would do well to avoid that semantic slip even if you do know it.

[1] languages with ref parameter semantics alter this but we are discussing java here.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Java passes objects as a reference

No, it simply passes them as references (passing by value when it does so). These are opaque, and will behave correctly in the face of the runtime changing the world behind your back. But to all intents an purposes they can be thought of as functioning like the 'pointing' (as opposed to arithmetic) aspects of pointers.

Such things are a critical aspect of almost all imperative programming languages in use today so I think you should familiarise yourself with them.

I don't suggest you use static variables unless the thing being used is truly global, and unlikely t5o ever be anything but global (be careful here). you should try to pass them into the constructor where possible, and where not (many frameworks require empty constructors) you should either pass them in to methods which require them or provide some sort of 'init' method which is passed the instance before you need to use it. Obviously this latter solution is more complex since you have to ensure that you understand the state transitions involved on any instance of the object. Sometimes it is unavoidable though.
 

Chrisblue

Ars Legatus Legionis
12,154
ShuggyCoUk":23y4v60d said:
Java passes objects as a reference

No, it simply passes them as references (passing by value when it does so). These are opaque, and will behave correctly in the face of the runtime changing the world behind your back. But to all intents an purposes they can be thought of as functioning like the 'pointing' (as opposed to arithmetic) aspects of pointers.

Such things are a critical aspect of almost all imperative programming languages in use today so I think you should familiarise yourself with them.

I don't suggest you use static variables unless the thing being used is truly global, and unlikely t5o ever be anything but global (be careful here). you should try to pass them into the constructor where possible, and where not (many frameworks require empty constructors) you should either pass them in to methods which require them or provide some sort of 'init' method which is passed the instance before you need to use it. Obviously this latter solution is more complex since you have to ensure that you understand the state transitions involved on any instance of the object. Sometimes it is unavoidable though.
Ah alright then. This this is what I was reading just for reference.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
The article you linked to is correct, you just read it wrong, or got confused by the use of the term reference in two different concepts.

There is the concept of a variable which holds some data, and may be changed to hold different data.[1]

The data may be a value type (in java the only ones are primitives) which mean the entirety of the value is stored within the variable wherever that variable may be. Or the variable is to a reference type, which means the variable stores the information required to 'get to' the object it refers to, and the data that makes up the object (its fields and metadata about it) is stored somewhere else (often called the free store or heap[2]. In java when you write foo.bar it means "find me the thing foo refers to (dereference) and get the field bar defied on it".

So variables hold values or references, which can be thought of as special kinds of values the runtime magics up for you when you do new Whatever(). The value they hold being the reference to the object created.

Now when you assign the value in a variable to another variable (either directly or by calling a function which takes arguments) the value contained in the first variable is *copied* into the target. This is pass by value. Even if hat value was a reference to some object somewhere all you do is end up with two variables which refer to the same object, which you could then dereference to get o the same object. If the types were value types there would be two variables, each holding their own, entirely independent copy of, let's say 9. You cannot change 9 into something else, you can only Chang the variable to hold something other than 9, let's say 4. By changing the second variable to be 4 you have done nothing to the first variable, I is still holding 9.

The same mechanics apply to variables containing references. If we change the second variable to refer to a new Whatever() then the original variable is unaffected, it still refers to the original.

Some languages allow pass by reference. What this means is that there are not two variables, there is only one, it just has two different names, a change to the value via one name implicitly changes the other (since they are just two different ways to refer to the same variable). This uses the term "reference" again because, the second variable is really just a reference to another variable.

Thus you have reference types and value types, plus pass by value and pass by reference and you can mix and match the two entirely independently in many languages. Java, as with so much of its design, goes with the the simple, one size fits almost everybody but not very prettily approach[3] and says: there is only one way to pass variables in function calls, by value.

It chickens out of going the whole hog on the different types though. There are value types in java but you're not trusted to define the, only the designers were allowed and so there are the primitive types (booleans, ints, floats etc), thus java has pass by value on value types and pass by value on reference types.

1. Let's ignore final for now
2. This is often true but should be viewed as an implementation detail, the important thing is not where the object is, it is the lifetime of the object.
3. I am being mean mainly for comic effect here, don't take this too seriously just now

Edit: the typos were bad, sorry
 

agifford

Ars Tribunus Militum
2,142
Christopher Blue":keyo3wu9 said:
No you're right and I had just remembered static variables. I think I can do what I need to do with some static variables.

In fact I just got done reading a refresher on how Java passes objects as a reference to a pointer. A bit confusing if you ask me but you get used to it.


no, you should forget static variables. You won't be able to create two different instances of a gui element once you start using them.

As mentioned elsewhere, mvc is worth looking into. Or just creating a data object thats passed into both of your gui objects and referencing the data off that.
 
Status
Not open for further replies.