Data in new object overwrites existing objects?

Ja Molzahn

New Member
This is the calling class:\[code\]public class TestUseMyObject {public static void main(String[] args) throws Exception { UseMyObject li=new UseMyObject(); li.AddObject(null,"text","foo","Bar"); li.AddObject(null,"password","Password","Passedout"); li.AddObjectButton(null,"button","MyButton"); li.ShowInputs();}}\[/code\]It produces this output:\[code\]Name foo Type text Kind 1 //input[type='text' name='foo'foo Set to Baridx=1Name Password Type password Kind 1 //input[type='password' name='Password'Password Set to Passedoutidx=2Name MyButton Type button Kind 0 //input[type='button' name='MyButton'idx=30 Value Passedout Name MyButton Type button Kind 0 //input[type='button' name='MyButton'1 Value Passedout Name MyButton Type button Kind 0 //input[type='button' name='MyButton'2 Value Passedout Name MyButton Type button Kind 0 //input[type='button' name='MyButton\[/code\]The other classes are:\[code\]public class UseMyObject {private static MyObject Inputs[];private static int idx;public UseMyObject(){ Inputs=new MyObject[10]; idx=0; Inputs[idx]=null;}public void AddObject( String id, String type, String name, String value ){ Inputs[idx]=new MyObject (1, id,type,name); Inputs[idx++].setText(value); System.out.println("idx="+idx);}public void AddObjectButton( String id, String type, String name){ Inputs[idx++]=new MyObject (0, id,type,name); System.out.println("idx="+idx);}public void ShowInputs () { for(int i =0; i< idx; i++){ Inputs.ShowInfo(i); }}}public class MyObject {private static String Id,Type,Name,Xpath, Value;public static int Kind; //button or textpublic MyObject(int kind, String id, String type, String name) { this.Kind=kind; if(null != id) this.Id=new String (id); if(null != type) this.Type=new String(type); if(null != name) this.Name=new String(name); Xpath="//input["; if ( null != Id ) Xpath+="id='"+Id+"' "; if (null != Type ) Xpath+="type='"+Type+"' "; if(null != Name) Xpath+="name='"+Name+"'"; System.out.println("Name "+Name+" Type "+Type+" Kind "+Kind+" "+Xpath);}public void ShowInfo (int i) { System.out.println(i+" Value "+Value+" Name "+Name+" Type "+Type+" Kind "+Kind+" "+Xpath);}public void setText (String text) { this.Value=http://stackoverflow.com/questions/15563371/new String(text); if(null != Name) System.out.println(Name+" Set to "+Value); else System.out.println("null Set to "+text);}}\[/code\]Anyone have a clue as to what is going on? I'm an experienced C programmer, and I thought I understood Java's objects, but clearly I'm missing something important. It appears that data from the last object added is copied to the existing objects, which I thought was not possible in this context. The calling class is in a different package than MyObject and UseMyObject.
 
Back
Top