Why the first swap attempt works, but the second doesn't work? [duplicate]









up vote
-1
down vote

favorite













This question already has an answer here:



  • Is Java “pass-by-reference” or “pass-by-value”?

    76 answers



  • Java: Why does this swap method not work? [duplicate]

    10 answers



package main;

public class Main
double radius;
public Main(double newRadius)
radius = newRadius;



public static void main (String args)
Main x = new Main(1);
Main y = new Main(2);
Main temp;
// try to swap first time
temp = x;
x = y;
y = temp;
System.out.println(x.radius + " " + y.radius);
x = new Main(1);
y = new Main(2);
// try to swap second time
swap(x, y);
System.out.println(x.radius + " " + y.radius);

public static void swap(Main x, Main y)
Main temp = x;
x = y;
y = temp;





Why the first time worked, but the second time didn't? The first one did the swap, but the second one didn't. I'm passing the reference to the function. Why this doesn't work?










share|improve this question













marked as duplicate by Mureinik java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 10 at 13:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 5




    References are passed by value. The variables inside the method are not the variables outside the method, so reassigning them doesn't alter external variables. See Is Java pass-by-reference or pass-by-value?
    – khelwood
    Nov 10 at 13:11















up vote
-1
down vote

favorite













This question already has an answer here:



  • Is Java “pass-by-reference” or “pass-by-value”?

    76 answers



  • Java: Why does this swap method not work? [duplicate]

    10 answers



package main;

public class Main
double radius;
public Main(double newRadius)
radius = newRadius;



public static void main (String args)
Main x = new Main(1);
Main y = new Main(2);
Main temp;
// try to swap first time
temp = x;
x = y;
y = temp;
System.out.println(x.radius + " " + y.radius);
x = new Main(1);
y = new Main(2);
// try to swap second time
swap(x, y);
System.out.println(x.radius + " " + y.radius);

public static void swap(Main x, Main y)
Main temp = x;
x = y;
y = temp;





Why the first time worked, but the second time didn't? The first one did the swap, but the second one didn't. I'm passing the reference to the function. Why this doesn't work?










share|improve this question













marked as duplicate by Mureinik java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 10 at 13:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 5




    References are passed by value. The variables inside the method are not the variables outside the method, so reassigning them doesn't alter external variables. See Is Java pass-by-reference or pass-by-value?
    – khelwood
    Nov 10 at 13:11













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite












This question already has an answer here:



  • Is Java “pass-by-reference” or “pass-by-value”?

    76 answers



  • Java: Why does this swap method not work? [duplicate]

    10 answers



package main;

public class Main
double radius;
public Main(double newRadius)
radius = newRadius;



public static void main (String args)
Main x = new Main(1);
Main y = new Main(2);
Main temp;
// try to swap first time
temp = x;
x = y;
y = temp;
System.out.println(x.radius + " " + y.radius);
x = new Main(1);
y = new Main(2);
// try to swap second time
swap(x, y);
System.out.println(x.radius + " " + y.radius);

public static void swap(Main x, Main y)
Main temp = x;
x = y;
y = temp;





Why the first time worked, but the second time didn't? The first one did the swap, but the second one didn't. I'm passing the reference to the function. Why this doesn't work?










share|improve this question














This question already has an answer here:



  • Is Java “pass-by-reference” or “pass-by-value”?

    76 answers



  • Java: Why does this swap method not work? [duplicate]

    10 answers



package main;

public class Main
double radius;
public Main(double newRadius)
radius = newRadius;



public static void main (String args)
Main x = new Main(1);
Main y = new Main(2);
Main temp;
// try to swap first time
temp = x;
x = y;
y = temp;
System.out.println(x.radius + " " + y.radius);
x = new Main(1);
y = new Main(2);
// try to swap second time
swap(x, y);
System.out.println(x.radius + " " + y.radius);

public static void swap(Main x, Main y)
Main temp = x;
x = y;
y = temp;





Why the first time worked, but the second time didn't? The first one did the swap, but the second one didn't. I'm passing the reference to the function. Why this doesn't work?





This question already has an answer here:



  • Is Java “pass-by-reference” or “pass-by-value”?

    76 answers



  • Java: Why does this swap method not work? [duplicate]

    10 answers







java oop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 13:09









Roy Derek

6




6




marked as duplicate by Mureinik java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 10 at 13:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Mureinik java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 10 at 13:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 5




    References are passed by value. The variables inside the method are not the variables outside the method, so reassigning them doesn't alter external variables. See Is Java pass-by-reference or pass-by-value?
    – khelwood
    Nov 10 at 13:11













  • 5




    References are passed by value. The variables inside the method are not the variables outside the method, so reassigning them doesn't alter external variables. See Is Java pass-by-reference or pass-by-value?
    – khelwood
    Nov 10 at 13:11








5




5




References are passed by value. The variables inside the method are not the variables outside the method, so reassigning them doesn't alter external variables. See Is Java pass-by-reference or pass-by-value?
– khelwood
Nov 10 at 13:11





References are passed by value. The variables inside the method are not the variables outside the method, so reassigning them doesn't alter external variables. See Is Java pass-by-reference or pass-by-value?
– khelwood
Nov 10 at 13:11













1 Answer
1






active

oldest

votes

















up vote
0
down vote













Your mistaking how references are passed, your making a scope where references are swapped and then that scope ends.



Try storing the value of the field in the variable e.g. temp = x.radius and then assign to y.radius.



The reason why it works the first time is that the scope is the same.






share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Your mistaking how references are passed, your making a scope where references are swapped and then that scope ends.



    Try storing the value of the field in the variable e.g. temp = x.radius and then assign to y.radius.



    The reason why it works the first time is that the scope is the same.






    share|improve this answer


























      up vote
      0
      down vote













      Your mistaking how references are passed, your making a scope where references are swapped and then that scope ends.



      Try storing the value of the field in the variable e.g. temp = x.radius and then assign to y.radius.



      The reason why it works the first time is that the scope is the same.






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Your mistaking how references are passed, your making a scope where references are swapped and then that scope ends.



        Try storing the value of the field in the variable e.g. temp = x.radius and then assign to y.radius.



        The reason why it works the first time is that the scope is the same.






        share|improve this answer














        Your mistaking how references are passed, your making a scope where references are swapped and then that scope ends.



        Try storing the value of the field in the variable e.g. temp = x.radius and then assign to y.radius.



        The reason why it works the first time is that the scope is the same.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 14:33


























        community wiki





        2 revs
        Jay














            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo