Announcement Announcement Module
Collapse
No announcement yet.
Scope of command Object in a Validator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scope of command Object in a Validator

    In a Validation class is it possible to change the values of the command object? I am assigning a new Object to the command Object like so :

    Code:
    command = result.getUser();
    I thought the command Object would have been passed by reference, so all other references to the command Object would see the new values. However, this does not seem to happen. Maybe the command Object is being cloned when it is passed to the validator?

    Thanks

  • #2
    I suggest a read on objects and passing something by reference...

    Code:
    Date d1 = new Date(1,1,1);
    Date d2 = d1;
    d2 = new Date(2,2,2);
    In the sample above d1 is still 1,1,1 and d2 is now 2,2,2... They aren't both 2,2,2....

    Comment


    • #3
      So this would be more appropriate...

      Code:
      public class Main 
      {
      	public static void main(String[] args) {
      		new Main();
      	}
      
      	public Main(){
      		Box box = new Box(1);
      		System.out.println("Contents of box is " + box.getContents());
      		method(box);
      		System.out.println("Contents of box is " + box.getContents());
      	}
      
      	private void method(Box box){
      		box.setContents(2);
      	}
      
      	private class Box{
      
      		private int contents;
      
      		public Box(){
      
      		}
      
      		public Box(int contents){
      			this.contents = contents;
      		}
      
      		public void setContents(int contents){
      			this.contents = contents;		
      		}
      
      		public int getContents(){
      			return contents;
      		}
      
      	}
      }

      Comment

      Working...
      X