Results 1 to 3 of 3

Thread: Scope of command Object in a Validator

  1. #1
    Join Date
    Nov 2010
    Posts
    6

    Question 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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....
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2010
    Posts
    6

    Default

    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;
    		}
    
    	}
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •