Best way to share Java variables between classes -
i have odd problem in java. can solve solution seems inefficient.
i have class, simplified is
class zot { double edges[]; }
the problem in many cases want instance , instance b share 1 of edge instances. in other words, instance may allocate edge[2] , want instance b share edge[2]. easy enough in can set instance b point @ instance's edge[2]. how can efficiently? if allocate edges[] in instance a, can assign b's instance point a. want allocate single edge (e.g. edge[2]) in , assign in b. in java, 1 cannot (as far know) allocate single member of array (as 1 can in c++). ideally, want allocate useful member , assign it. could, of course, allocate 4 members, assign ones need, set unwanted members null , let gc clean up, if there hundreds or thousands of instances seems clumsy.
suggestions?
you can declare , allocate double edges[]
outside of both classes, pass array parameter in constructor both of instances want share it.
in java array object. when make instance double edges[] = new double[2];
edges
passed around pointer, not copy.
this means if make change in array in class a, class b see change.
Comments
Post a Comment