raja singh kumar wrote:My question is in this waiter and notifier example in lines 25 and 50 we are calling synchronized on the variable message but it is a private instance variable "message" in both Waiter and Notifier classes. So how does changing instance variable "message" in Notifier Class change the value of message in the Waiter class? I know it is a silly question but there is some gap in my understanding
raja singh kumar wrote:
How is it that it is in conflict with the example I used in my first post of this thread.
raja singh kumar wrote:By conflict I mean that in the second example which I posted, change in 1 reference variable of one class does not affect the reference variable of another class. But the first example shows that change in reference variable of one class changes the other as well since both reference variables point to the modified object.
I need some clarity here
raja singh kumar wrote:No, you misunderstood me.
My question is why there is inconsistency in the behavior of the two examples?
raja singh kumar wrote:
When I modify the message object it reflects in all classes but when I modify the string object it does not reflect in other classes?
No. You are creating a String with a literal; as far as the JVM is concerned, the String Literal "Hello " does not tell it to create a String object. That String literal IS A String object to the JVM. Have a look at the Java® Language Specification:
raja singh kumar wrote:. . . this.name="Hello "+name; and I have not used new operator in this line of code.
Then you are using the concatenation operator (+) on that String with another operand, which happens to be a String too. That JLS section will tell you that:-
A string literal is a reference to an instance of class String
Since you are not using a constant expression (sometimes called a compile time constant), that expression with + creates a new String and there is no need to use the new operator. Nor are you changing the values of any Strings if the result is “newly created.”
The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. . . .
The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).
raja singh kumar wrote:How many objects are created when I use the following line of code and what are they? The value assigned to name is "Raja".
We have four objects visible: look at lines 4: 7: 11: and 26: Since we are including "Hello, " this time, that is one more than you had in your code. The putfield instruction in 29: means the field and the object created in 26: are both the same object.
javap -c StringConcatenationDemo
Yes, 3, but that is different from what you said at first.
raja singh kumar wrote: . . . if I consider only String objects created in the above example, then there are 3. . . .
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton