• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding changing static variable output

 
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm having trouble understanding this piece of code.

ResultString Class:


PrintStrings Class:


Output:


I don't understand line 29, where Good Day is printed twice. Am I right in saying on line 25, that because strInMethod is empty, it clears "result"?


Any help is appreciated, thanks
 
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't understand line 29, where Good Day is printed twice. Am I right in saying on line 25, that because strInMethod is empty, it clears "result"?



strInMethod is empty



line 25: result = strInMethod;

strInMethod is a new instance of ResultString class. And the instance variable str (a String) is initialized to its default value: null (this is because no value is set, yet). And, both the references "result" and "strInMethod" are pointing to the same object with empty/null values for "str".

Place this statement after line 25 and run the program.
System.out.println(result.str); //  this will print null

I don't understand line 29, where Good Day is printed twice.



After line 25:

On line 29 the variable strInMethod.str is printed. This shows same value as that of the result.str in the previous line.


EDIT:
Finally, there are no static variables used in the classes, as mentioned in the topic title.
- PrintStrings: All variables are local (method) variables.
- ResultString: All variables are instance variables.
 
author
Posts: 14
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is often a cause of confusion in Java, especially if you are coming from a different language. On line 25 you are not doing a "copy-assignment" and taking a copy of StrInMethod, instead what you are doing here is copying the reference to that same String object on the heap.

So you have the situation [from line 25] where:

strInMethod Reference -> ResultString Object (created on line 19)
result Reference -> ResultString Object (created on line 19)

In line 26 you add Good Day to ResultString Object from line 19, and in line 28 you are effectively are appending to the same object via the reference assignment in line 25.

Hope that helps.
 
Caiz Austin
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both, I understand that bit about referencing now!! Now, I'm confused about why in the main, didn't "aString" change even though it was passed through the method with the strInMain object. StrInMain went from "Hello" to "Hello, how are you?", but aString stayed the same. I would have expected aString to come out as "Hi, how are you?"
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, some things to clear about objects and strings:

Objects of ResultString:

ResultString rString = new ResultString();

  • ResultString is a class.
  • rString is a reference variable (a pointer to an object).
  • new ResultString() creates an object, and is assigned to the rString.
  • str in the ResultString class is an instance variable. It represents the state in the object.


  • In Java objects, in general, are mutable. Means, their state can change without changing the reference to it (re-assigning).


    Here, only the state is changed with a new assigned value. Not the object.

    Strings:

    Strings in Java are not-mutable; immutable. They can't change on their own; to change values the modified value need to be re-assigned.






    PrintStrings example:

    Now, in the PrintStrings example this is what is happening to the aString:




    strInMain's change in state:


    reply
      Bookmark Topic Watch Topic
    • New Topic