views:

313

answers:

3

Sorry this is kinda long, but I needed to get the right scenario. This outputs all C's, why?? thanks in advance

import java.util.Hashtable;

public class Main {  
    public static ContainsTheHash containsthehash = new ContainsTheHash();  
    public static StoresValues storesvalues = new StoresValues();  
    public static GetsValuesAndPrints getsvaluesandprints = new GetsValuesAndPrints();  
    public static void main(String[] args) {}  

}

class ContainsTheHash {

    Hashtable script_code = new Hashtable();  
    public Contains_The_Hash() {};  
    public void put(long key, Script_Hash_Type sht){script_code.put(key, sht);}  
    public ScriptHashType get(long key){return (Script_Hash_Type)script_code.get(key);}  

}

class ScriptHashType {

     String string;  
     public ScriptHashType(){}  
     public String getstring () {return string;}  
     public void setstring(String str){string = str;}  

}



 class StoresValues {

     public StoresValues(){
         put();
     }
     public void put(){


          ScriptHashType sht = new ScriptHashType();  
          sht.setstring("A");  
          Main.contains_the_hash.put(1,sht);  
          sht.setstring("B");  
          Main.contains_the_hash.put(2,sht);  
          sht.setstring("C");  
          Main.contains_the_hash.put(3,sht);  
     }  

}

class GetsValuesAndPrints {

    public GetsValuesAndPrints(){  

           //should print "A\n B\n  C\n"  
           long temp = 1;  
           System.out.println(get(temp));  
           temp = 2;  
           System.out.println(get(temp));  
           temp = 3;  
           System.out.println(get(temp));  
    };


    public String get(long key){  

        return new String(((Script_Hash_Type)Main.contains_the_hash.get(key)).getstring());  

   }
}
+6  A: 

Change :

ScriptHashType sht = new ScriptHashType();
sht.setstring("A");
Main.contains_the_hash.put(1,sht);
sht.setstring("B");
Main.contains_the_hash.put(2,sht);
sht.setstring("C");
Main.contains_the_hash.put(3,sht);

to

ScriptHashType sht = new ScriptHashType();
sht.setstring("A");
Main.contains_the_hash.put(1,sht);
sht = new ScriptHashType();
sht.setstring("B");
Main.contains_the_hash.put(2,sht);
sht = new ScriptHashType();
sht.setstring("C");
Main.contains_the_hash.put(3,sht);

In the first piece of code You're updating the same object each time

Glen
To explain this, collections store references to objects (not copies of them), so you had 3 references to the same object in the hash.
R. Bemrose
Same thing I was going to reply. Your updating the string value within the same object. YOu end up with a single ScriptHashType instance set to "C" instead of 3 instances set to "A", "B", and "C".
rally25rs
I agree, that was my first comment. Yet, Is this a good design ? I find the code really weird
LB
The code *is* extremely weird.
Michael Borgwardt
lol, the code is actually a skeleton of a much more complicated program. so that might explain why its wierd.
+3  A: 
class StoresValues {

     public StoresValues() {
         put();
     };

     public void put() {
          ScriptHashType sht = new ScriptHashType();  
          sht.setstring("A");  
          Main.contains_the_hash.put(1,sht);  
          sht.setstring("B");  
          Main.contains_the_hash.put(2,sht);  
          sht.setstring("C");  
          Main.contains_the_hash.put(3,sht);  
     }

You build only one single object, sht, using the new operator. You add that object 3 times. The last setstring on that object sets the string to "C". As there is only 1 object sht that single object will have the value "C".

What you should have done is something like:

 public void put() {
  ScriptHashType sht = new ScriptHashType();  
  sht.setstring("A");  
  Main.contains_the_hash.put(1,sht);  
  sht = new ScriptHashType();  
  sht.setstring("B");  
  Main.contains_the_hash.put(2,sht);  
  sht = new ScriptHashType();  
  sht.setstring("C");  
  Main.contains_the_hash.put(3,sht);  
 }
extraneon
ill give that a try
A: 

You are only ever storing one instance of ScriptHashType into the Hashtable and then manipulating its value. To get the behavior you are expecting you would need to create three instances of ScriptHashType, one for each desired value.

laz
Thank you very much, everyone's comments have been sooo helpful, and responded so fast too. I did not realize that I had to have multiple instances of scripthashtype. I was using the hashtable improperly.Again thanks