Wrapper class problem

I want some information regarding wrapper class. If I pass Integer object to a method as an argument and I want to change the value of that Integer object, so that it will reflect from where it is called.
If  I put following code   T=newInteger((int)T.intValue()+5)); in the method, where T is the Integerargument came, it will not affact to the method from where it is called.bcoz it is creating new object and not refrencing to original objectT.

So how shall I modify it in that method so that it will reflect from where I have called.

Umapada Manna.

You can change the value of that reference byreturning the reference of new Object that you have created inmethod.
----------------------------------------------
  public classTest4{
      public static void main(String[] args){
 Integer S = newInteger(5);
 S=myMethod(S);
   System.out.println("Main : "+S);
     }

 public static Integer myMethod(Integer T)
        {
               T=new Integer(T.intValue()+5);
  return T;
 }
  }
------------------------------------------------
V.V. NageshAkula.

ya, It is fine. But is there any way to change to that particular argument object T, instead creating new object and returning that one. Is wrapper class provide any method to change its content, after once created.

Umapada Manna.

Try this,

        public static Integer myMethod(Integer T)
        {
                int val = T.intValue() + 5 ;
                return T.decode(String.valueOf(val));
        }

Pallav

class TEST extends Object{
   public void F1( Integer S ){
     S  = new Integer((short)(S.intValue() + 5)) ;

   }
   public void Result( int  x ) {
     Integer X = new Integer((short) x ) ;
     F1( X ) ;
     System.out.println( "Value is "  + X ) ;
     // I want to  print 25 instead of 20        here  ------------(1)
   }
   public static void main(String arg[]){
   TEST e=new TEST();
   e.Result(20);
    //System.out.println(e.Result(3));
   }
}

decode is used to Decodes a String into a Integer. so it won't work !

Umapada Manna.

You can use,
Integer.valueOf(int val)

Pallav

I do not think it is possible to retrieve the value of S at point (1) in your program because once the function F1 is executed S loses its reference to the object pointed by X and you do not have a reference to the S object in your function Result.

It could have been possible to retreive the value of S object if function F1 was'nt void.

It could also have been possible to do the same if Java passed objects by reference but Java does not pass objects by reference, it passes object references by value.

Vinay.

You can’t do this until you return an Integer Object and collect it in calling method.

Reason: All Wrapper Classes are Immutable.

If you are not sure what’s exactly immutability is, then understand first.

There is no setter method in any Wrapper Class. Once created can’t be changed.

Sanjeev

Thanks Sanjeeb. Now it is clear. Yes ! Wrapper classes are Immutable. That clear evrything to me. So only way by returning that wrapper object.

Umapada Manna.

Wrapper classes are immutable! That's the polished answer. Thx for that.

Vinay.

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.