Scala case class with cyclic referencing and value updationIs Java “pass-by-reference” or “pass-by-value”?Is the Scala 2.8 collections library a case of “the longest suicide note in history”?Difference between object and class in ScalaWhat is the difference between Scala's case class and class?How to clone a case class instance and change just one field in Scala?Why do val and def implement abstract methods at different times?scala overriding class parametersInvoking reflected case class constructor in Scala, Part 2Scala Multiple Nested Case ClasesWhy is the each iteration parameter val and not var in for loop in scalaIs it possible to declare a val before assignment/initialization in Scala?

Factorio analysis: data munging

How to determine if window is maximised or minimised from bash script

What Brexit solution does the DUP want?

Divisibility of sum of multinomials

Is there a familial term for apples and pears?

Why does the small signal analysis work? (Intuition)

How to make payment on the internet without leaving a money trail?

Does the radius of the Spirit Guardians spell depend on the size of the caster?

Email Account under attack (really) - anything I can do?

Should I join an office cleaning event for free?

Circuitry of TV splitters

Why do we use polarized capacitor?

Weird behaviour when using querySelector

What typically incentivizes a professor to change jobs to a lower ranking university?

Can town administrative "code" overule state laws like those forbidding trespassing?

Copenhagen passport control - US citizen

Shell script can be run only with sh command

Example of a relative pronoun

"The augmented fourth (A4) and the diminished fifth (d5) are the only augmented and diminished intervals that appear in diatonic scales"

Where to refill my bottle in India?

XeLaTeX and pdfLaTeX ignore hyphenation

DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

Why is an old chain unsafe?



Scala case class with cyclic referencing and value updation


Is Java “pass-by-reference” or “pass-by-value”?Is the Scala 2.8 collections library a case of “the longest suicide note in history”?Difference between object and class in ScalaWhat is the difference between Scala's case class and class?How to clone a case class instance and change just one field in Scala?Why do val and def implement abstract methods at different times?scala overriding class parametersInvoking reflected case class constructor in Scala, Part 2Scala Multiple Nested Case ClasesWhy is the each iteration parameter val and not var in for loop in scalaIs it possible to declare a val before assignment/initialization in Scala?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-2















Im trying to understand the following code, how the value in the variables a of case class dummy is being updated in the following code.



In the value method, I'm assigning the value of a to b , and adding new Node to b, which inturn it is reflecting the changes in a also. I don't understand how the value is a is being changed without even re-assignement.



object test 
def main(args: Array[String])

case class abc(str: String, var ele: abc)

case class dummy(a: abc)
def value() =
var b = a
println(s"Value of B before changing : $b.str") // Value of B before changing : Good
println(s"Value of A before changing : $a.str") // Value of A before changing : Good
val newObj = abc(" Morning", null)
b.ele = newObj
println(s"Value of A After changing : $a.str,$a.ele.str") // Value of A After changing : Good, Morning
println(s"Value of B after changing : $b.str,$b.ele.str") // Value of B after changing : Good, Morning



val testObj = dummy(abc("Good", null))
testObj.value()











share|improve this question






























    -2















    Im trying to understand the following code, how the value in the variables a of case class dummy is being updated in the following code.



    In the value method, I'm assigning the value of a to b , and adding new Node to b, which inturn it is reflecting the changes in a also. I don't understand how the value is a is being changed without even re-assignement.



    object test 
    def main(args: Array[String])

    case class abc(str: String, var ele: abc)

    case class dummy(a: abc)
    def value() =
    var b = a
    println(s"Value of B before changing : $b.str") // Value of B before changing : Good
    println(s"Value of A before changing : $a.str") // Value of A before changing : Good
    val newObj = abc(" Morning", null)
    b.ele = newObj
    println(s"Value of A After changing : $a.str,$a.ele.str") // Value of A After changing : Good, Morning
    println(s"Value of B after changing : $b.str,$b.ele.str") // Value of B after changing : Good, Morning



    val testObj = dummy(abc("Good", null))
    testObj.value()











    share|improve this question


























      -2












      -2








      -2








      Im trying to understand the following code, how the value in the variables a of case class dummy is being updated in the following code.



      In the value method, I'm assigning the value of a to b , and adding new Node to b, which inturn it is reflecting the changes in a also. I don't understand how the value is a is being changed without even re-assignement.



      object test 
      def main(args: Array[String])

      case class abc(str: String, var ele: abc)

      case class dummy(a: abc)
      def value() =
      var b = a
      println(s"Value of B before changing : $b.str") // Value of B before changing : Good
      println(s"Value of A before changing : $a.str") // Value of A before changing : Good
      val newObj = abc(" Morning", null)
      b.ele = newObj
      println(s"Value of A After changing : $a.str,$a.ele.str") // Value of A After changing : Good, Morning
      println(s"Value of B after changing : $b.str,$b.ele.str") // Value of B after changing : Good, Morning



      val testObj = dummy(abc("Good", null))
      testObj.value()











      share|improve this question
















      Im trying to understand the following code, how the value in the variables a of case class dummy is being updated in the following code.



      In the value method, I'm assigning the value of a to b , and adding new Node to b, which inturn it is reflecting the changes in a also. I don't understand how the value is a is being changed without even re-assignement.



      object test 
      def main(args: Array[String])

      case class abc(str: String, var ele: abc)

      case class dummy(a: abc)
      def value() =
      var b = a
      println(s"Value of B before changing : $b.str") // Value of B before changing : Good
      println(s"Value of A before changing : $a.str") // Value of A before changing : Good
      val newObj = abc(" Morning", null)
      b.ele = newObj
      println(s"Value of A After changing : $a.str,$a.ele.str") // Value of A After changing : Good, Morning
      println(s"Value of B after changing : $b.str,$b.ele.str") // Value of B after changing : Good, Morning



      val testObj = dummy(abc("Good", null))
      testObj.value()








      scala






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 9 at 4:20









      Er. Dinesh

      172114




      172114










      asked Mar 9 at 4:10









      Guda uma shankerGuda uma shanker

      659




      659






















          1 Answer
          1






          active

          oldest

          votes


















          2














          var b = a


          This does not make a copy.



          You now have both variables pointing to the same object, and any change made to the state of b will also reflect in a.



          For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var in it, it would be immutable and you can be sure that no matter who you give your a to, it will still be intact when you get it back.



          (The second bad habit to shake is using null. No need for that in Scala, make the type an Option instead).






          share|improve this answer























          • as you have said , var b = a 'doesn't make a copy' is this statement valid only for case class with contains mutable fields.

            – Guda uma shanker
            Mar 9 at 4:31






          • 1





            It is the same for all objects (including arrays). A = just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).

            – Thilo
            Mar 9 at 4:37












          • I tried the same for this code var a = 2; var b = a; b = 3; println(a,b) // (2,3) , according to statement it should update the value in a also, but it is not doing .

            – Guda uma shanker
            Mar 9 at 4:41







          • 1





            b = 3 does not update the object that b is pointing to. It sets b to another value. That does not affect other variables (like a) that happen to also contain the old value. Your original code had b.ele = newObj (NOT b = newObj). That kept b pointing at the same object (which a was also looking at), but updated ele inside of that object (and thus, also a.ele which at that point was just another name/variable/label for the same thing).

            – Thilo
            Mar 9 at 5:04












          • Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.

            – Thilo
            Mar 9 at 5:05












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073906%2fscala-case-class-with-cyclic-referencing-and-value-updation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          var b = a


          This does not make a copy.



          You now have both variables pointing to the same object, and any change made to the state of b will also reflect in a.



          For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var in it, it would be immutable and you can be sure that no matter who you give your a to, it will still be intact when you get it back.



          (The second bad habit to shake is using null. No need for that in Scala, make the type an Option instead).






          share|improve this answer























          • as you have said , var b = a 'doesn't make a copy' is this statement valid only for case class with contains mutable fields.

            – Guda uma shanker
            Mar 9 at 4:31






          • 1





            It is the same for all objects (including arrays). A = just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).

            – Thilo
            Mar 9 at 4:37












          • I tried the same for this code var a = 2; var b = a; b = 3; println(a,b) // (2,3) , according to statement it should update the value in a also, but it is not doing .

            – Guda uma shanker
            Mar 9 at 4:41







          • 1





            b = 3 does not update the object that b is pointing to. It sets b to another value. That does not affect other variables (like a) that happen to also contain the old value. Your original code had b.ele = newObj (NOT b = newObj). That kept b pointing at the same object (which a was also looking at), but updated ele inside of that object (and thus, also a.ele which at that point was just another name/variable/label for the same thing).

            – Thilo
            Mar 9 at 5:04












          • Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.

            – Thilo
            Mar 9 at 5:05
















          2














          var b = a


          This does not make a copy.



          You now have both variables pointing to the same object, and any change made to the state of b will also reflect in a.



          For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var in it, it would be immutable and you can be sure that no matter who you give your a to, it will still be intact when you get it back.



          (The second bad habit to shake is using null. No need for that in Scala, make the type an Option instead).






          share|improve this answer























          • as you have said , var b = a 'doesn't make a copy' is this statement valid only for case class with contains mutable fields.

            – Guda uma shanker
            Mar 9 at 4:31






          • 1





            It is the same for all objects (including arrays). A = just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).

            – Thilo
            Mar 9 at 4:37












          • I tried the same for this code var a = 2; var b = a; b = 3; println(a,b) // (2,3) , according to statement it should update the value in a also, but it is not doing .

            – Guda uma shanker
            Mar 9 at 4:41







          • 1





            b = 3 does not update the object that b is pointing to. It sets b to another value. That does not affect other variables (like a) that happen to also contain the old value. Your original code had b.ele = newObj (NOT b = newObj). That kept b pointing at the same object (which a was also looking at), but updated ele inside of that object (and thus, also a.ele which at that point was just another name/variable/label for the same thing).

            – Thilo
            Mar 9 at 5:04












          • Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.

            – Thilo
            Mar 9 at 5:05














          2












          2








          2







          var b = a


          This does not make a copy.



          You now have both variables pointing to the same object, and any change made to the state of b will also reflect in a.



          For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var in it, it would be immutable and you can be sure that no matter who you give your a to, it will still be intact when you get it back.



          (The second bad habit to shake is using null. No need for that in Scala, make the type an Option instead).






          share|improve this answer













          var b = a


          This does not make a copy.



          You now have both variables pointing to the same object, and any change made to the state of b will also reflect in a.



          For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var in it, it would be immutable and you can be sure that no matter who you give your a to, it will still be intact when you get it back.



          (The second bad habit to shake is using null. No need for that in Scala, make the type an Option instead).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 9 at 4:19









          ThiloThilo

          197k79423577




          197k79423577












          • as you have said , var b = a 'doesn't make a copy' is this statement valid only for case class with contains mutable fields.

            – Guda uma shanker
            Mar 9 at 4:31






          • 1





            It is the same for all objects (including arrays). A = just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).

            – Thilo
            Mar 9 at 4:37












          • I tried the same for this code var a = 2; var b = a; b = 3; println(a,b) // (2,3) , according to statement it should update the value in a also, but it is not doing .

            – Guda uma shanker
            Mar 9 at 4:41







          • 1





            b = 3 does not update the object that b is pointing to. It sets b to another value. That does not affect other variables (like a) that happen to also contain the old value. Your original code had b.ele = newObj (NOT b = newObj). That kept b pointing at the same object (which a was also looking at), but updated ele inside of that object (and thus, also a.ele which at that point was just another name/variable/label for the same thing).

            – Thilo
            Mar 9 at 5:04












          • Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.

            – Thilo
            Mar 9 at 5:05


















          • as you have said , var b = a 'doesn't make a copy' is this statement valid only for case class with contains mutable fields.

            – Guda uma shanker
            Mar 9 at 4:31






          • 1





            It is the same for all objects (including arrays). A = just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).

            – Thilo
            Mar 9 at 4:37












          • I tried the same for this code var a = 2; var b = a; b = 3; println(a,b) // (2,3) , according to statement it should update the value in a also, but it is not doing .

            – Guda uma shanker
            Mar 9 at 4:41







          • 1





            b = 3 does not update the object that b is pointing to. It sets b to another value. That does not affect other variables (like a) that happen to also contain the old value. Your original code had b.ele = newObj (NOT b = newObj). That kept b pointing at the same object (which a was also looking at), but updated ele inside of that object (and thus, also a.ele which at that point was just another name/variable/label for the same thing).

            – Thilo
            Mar 9 at 5:04












          • Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.

            – Thilo
            Mar 9 at 5:05

















          as you have said , var b = a 'doesn't make a copy' is this statement valid only for case class with contains mutable fields.

          – Guda uma shanker
          Mar 9 at 4:31





          as you have said , var b = a 'doesn't make a copy' is this statement valid only for case class with contains mutable fields.

          – Guda uma shanker
          Mar 9 at 4:31




          1




          1





          It is the same for all objects (including arrays). A = just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).

          – Thilo
          Mar 9 at 4:37






          It is the same for all objects (including arrays). A = just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).

          – Thilo
          Mar 9 at 4:37














          I tried the same for this code var a = 2; var b = a; b = 3; println(a,b) // (2,3) , according to statement it should update the value in a also, but it is not doing .

          – Guda uma shanker
          Mar 9 at 4:41






          I tried the same for this code var a = 2; var b = a; b = 3; println(a,b) // (2,3) , according to statement it should update the value in a also, but it is not doing .

          – Guda uma shanker
          Mar 9 at 4:41





          1




          1





          b = 3 does not update the object that b is pointing to. It sets b to another value. That does not affect other variables (like a) that happen to also contain the old value. Your original code had b.ele = newObj (NOT b = newObj). That kept b pointing at the same object (which a was also looking at), but updated ele inside of that object (and thus, also a.ele which at that point was just another name/variable/label for the same thing).

          – Thilo
          Mar 9 at 5:04






          b = 3 does not update the object that b is pointing to. It sets b to another value. That does not affect other variables (like a) that happen to also contain the old value. Your original code had b.ele = newObj (NOT b = newObj). That kept b pointing at the same object (which a was also looking at), but updated ele inside of that object (and thus, also a.ele which at that point was just another name/variable/label for the same thing).

          – Thilo
          Mar 9 at 5:04














          Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.

          – Thilo
          Mar 9 at 5:05






          Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.

          – Thilo
          Mar 9 at 5:05




















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073906%2fscala-case-class-with-cyclic-referencing-and-value-updation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived