It goes like this ...
First of all in java there is only "OBJECT REFERENCES" (reference to
an object)
there is no "REFERENCE OBJECT" (Object which refer to something )

in the below code there are totally 3 objects ,

public class MyClass()
{
int a;
int b;
MyClass(int x,int y)
{
a=x;
b=y;
}

          void swapObjects(MyClass obj)
          {
                MyClass temp = new MyClass();   //Selva: a new object
is created and referred by temp
                temp = this;                    //Selva: since the
temp reference is overwritten by this-> the newly created object is
abandoned -> it is lost
                this.a = obj.a;
                this.b  =obj.b;                 //Selva: values of c1
object is overwritten by values c2 -> now both the objects create in
main has same values
                obj=temp;                       //Selva: this make no
meaning as obj again refers temp(which is this(which is c1))
          }
public static void main(STring[] args)
{
  MyClass c1 = new MyClass(10,20);
MyClass c2 = new MyClass(15,30);
c1.swapObjects(c2); // here We pass c2 which is MyClass reference
object by  Selva:the is no reference object (remember no pointers in
java)
value ,is it correct ?

}
}
Regards
//Selva

On Mar 14, 12:14 am, sharmi n <[email protected]> wrote:
> Hi
> I have a question regarding pass by value and reference.
> Suppose I call a function that takes an object reference as argument. Then
> that object reference is still "passed by value" .
> (http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.htmlchk the
> "Passing Reference Data Type Arguments" paragraph in this link )
>
> *So is there some way in which we pass reference objects  "by reference"
> ,instead of "by value*".
> Or is there no such concept for objects
> eg.
>  public class MyClass()
> {
> int a;
> int b;
> MyClass(int x,int y)
> {
> a=x;
> b=y;}
>
>           void swapObjects(MyClass obj)
>           {
>                 MyClass temp = new MyClass();
>                 temp = this;
>                 this.a = obj.a;
>                 this.b  =obj.b;
>                 obj=temp;
>           }
> public static void main(STring[] args)
> {
>   MyClass c1 = new MyClass(10,20);
> MyClass c2 = new MyClass(15,30);
> c1.swapObjects(c2); // here We pass c2 which is MyClass reference object by
> value ,is it correct ?
>
> }
> }
>
> Thanks
> Sharmishta
>
> On Fri, Mar 13, 2009 at 6:39 AM, Selva <[email protected]> wrote:
>
> > Hi,
>
> > In both the cases  local memory variables are created (in the called-
> > method to hold the parameters being passed) .
>
> > Pass by value = the parameter value that is being passed will be a
> > primitive type; Hence there will be copy of value being passed
> > Pass by reference = the parameter  value that is being passed will be
> > reference(address) of an object. Hence there will be copy of reference
>
> > Hope this will help
> > :-)
>
> > On Mar 13, 9:54 am, CIA <[email protected]> wrote:
> > > See the link below:
> >http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
>
> > > That is pass by reference. It create more
> > > But now, I want to know how really pass by value does !
> > > Does it create one more cell in memory?
>
> > > Suppose, I have a variable:
> > >       int a = 10;
>
> > > then pass by value:
> > >      method(a);
>
> > > How does it work?
> > > Thanks for replying :)
>
>

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to