Passing a hashtable and retaining a copy?
I'm a little unclear about how parameters work in Java. I have a hashtable that contains some data. It gets passed into another class via a set method. The other class (B) takes the original hashtable and assigns it to a static field.
I"ll illustrate it better with some pseudocode:
class ClassA
{
// assume this gets populated at some point
privatestatic HashTable originalTable;
void someMethod()
{
ClassB.setTable(originalTable);
}
}
class ClassB
{
static HashTable myTable;
publicvoid setTable(Hashtable newTable)
{
myTable = newTable;
}
}
I thought all objects in Java were pass by reference. So, wouldn't ClassB.myTable get affected by any changes to ClassA.originalTable? I tried to clear ClassA.originalTable, but noticed that ClassB.myTable was unaffected.
Why is this?
> I thought all objects in Java were pass by reference.
Let's not get that started again. In your code, both originalTable and myTable will end up referencing the same map object:
import java.util.*;
class ClassA {
public static void main(String[] args) {
originalTable = new HashMap < String, String > ();
originalTable.put("1", "one");
originalTable.put("2", "two");
someMethod();
System.out.println("originalTable = " + originalTable);
System.out.println("myTable = " + ClassB.myTable);
System.out.println("mutating originalTable...");
originalTable.put("3", "three");
originalTable.put("2", "deuce");
System.out.println("originalTable = " + originalTable);
System.out.println("myTable = " + ClassB.myTable);
}
static Map < String, String > originalTable;
static void someMethod() {
ClassB.setTable(originalTable);
}
}
class ClassB {
static Map < String, String > myTable;
static void setTable(Map < String, String > newTable) {
myTable = newTable;
}
}
I'm still confused. If I clear originalTable in ClassA, then somewhere in ClassB I try to access myTable, shouldn't myTable also be cleared? When I do this however, its as if ClassB retained its own copy.
> I'm still confused. If I clear originalTable in
> ClassA, then somewhere in ClassB I try to access
> myTable, shouldn't myTable also be cleared? When I
> do this however, its as if ClassB retained its own
> copy.
Huh? Note how I post code that contradicts what you write, while you only
make vague statements :-)
import java.util.*;
class ClassA {
public static void main(String[] args) {
originalTable = new HashMap < String, String > ();
originalTable.put("1", "one");
originalTable.put("2", "two");
someMethod();
System.out.println("originalTable = " + originalTable);
System.out.println("myTable = " + ClassB.myTable);
System.out.println("clearing originalTable...");
originalTable.clear();
System.out.println("originalTable = " + originalTable);
System.out.println("myTable = " + ClassB.myTable);
}
static Map < String, String > originalTable;
static void someMethod() {
ClassB.setTable(originalTable);
}
}
class ClassB {
static Map < String, String > myTable;
static void setTable(Map < String, String > newTable) {
myTable = newTable;
}
}
> I'm still confused. If I clear originalTable in
> ClassA, then somewhere in ClassB I try to access
> myTable, shouldn't myTable also be cleared? When I
> do this however, its as if ClassB retained its own
> copy.
Nope.
import java.util.*;
class Foo {
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<String, String> ();
map.put("1", "one");
map.put("2", "two");
A a = new A(map);
B b = new B(map);
b.display();
a.clear();
b.display();
}
}
class A {
private Map map;
A(Map map) {
this.map = map;
}
void clear() {
map.clear();
}
}
class B {
private Map map;
B(Map map) {
this.map = map;
}
void display() {
System.out.println(map);
}
}
Output:
{2=two, 1=one}
{}
~
Thanks for the responses guys. I seem to have missed out on the important fact that the two Hashtables were set over IXC. So, with this, it really seems to be a copy made rather than both originalTable and newTable pointing to the same reference.
That's why they didn't seem to be affecting each other.