Been a long time since I had last touched Java. However I can perhaps
hazard an explanation. Forgive me if I make a mistake.

In a method, when you are sending an object reference, it is just like
a pointer(Actually a double pointer in a Java implementation).

So, the changes that you make to the OBJECT pointed to by the
reference changes the original object. However, if you change just the
reference pointer itself, you have lost it's earlier connection to the
original pointer.

In you method "Method", before entering, the parameter "st1" is
pointing to the local object "st1" of the "main" function(I wish you
had named them differently). But after, you assign it to the "st2"
local reference (which is pointing to "st2" of the 'main' function),
what you have done it is break it's earlier association, and make a
new one - the one pointed to by the "st2" local reference, which
incidently is pointing to "st2" of the "main" method.

So while the println inside "Method" work properly(st1 is a way
pointing to main:st2), since the changes have not been made to the
original object ("main:st1"), after the method returns, the original
object is unchanged.


In other words:

Method "main":
  Local reference: st1 ,st2

Method "Method":
  Before Entering:
    Local reference: st1->(main:st1)
                     st2->(main:st2)
  After (st1=st2):
                     st1->st2
                     st2->(main:st2)
  
- Sandip

On Wed, Feb 27, 2002 at 03:07:22PM +0530, mohit spoke out thus:
> A problem in running a java Program in Linux
> 
> import java.util.Stack;
> public class aClass {
>  public static void main(String []agrs) {
>   Stack st1 = new Stack();
>   Stack st2 = new Stack();
>   new aClass().Method(st1, st2);
>   System.out.println("st1 has: " + st1);
>   System.out.println("st2 has: " + st2);
>  }
>  private void Method(Stack st1,Stack st2) {
>   st2.push(new Integer(100));
>   st1 = st2;
>   System.out.println("st1 has: "+st1);
>   System.out.println("st2 has: "+st2);
>  }
> }
> 1
> The output of the program is strange , what could be the problem.
> It gives output as
> 
> st1 has: [100]
> st2 has: [100]
> st1 has: []
> st2 has: [100]
> 
> Can someone tell me why this output comes , why is st1 equal to [100] inside
> and [] outside.
> 
> From
> mOhit kHullar
> 
> 
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
> 
>           ================================================
> To subscribe, send email to [EMAIL PROTECTED] with subscribe in subject header
> To unsubscribe, send email to [EMAIL PROTECTED] with unsubscribe in subject 
>header
> Archives are available at http://www.mail-archive.com/ilugd%40wpaa.org
>           =================================================

-- 
Sandip Bhattacharya
sandipb @ bigfoot.com
http://www.sandipb.net
----------------------------

          ================================================
To subscribe, send email to [EMAIL PROTECTED] with subscribe in subject header
To unsubscribe, send email to [EMAIL PROTECTED] with unsubscribe in subject header
Archives are available at http://www.mail-archive.com/ilugd%40wpaa.org
          =================================================

Reply via email to