Help needed !!!!!

Hi,

The following code :

public class Test14{

static String s ="Instance";

public static void method(String s){

s+="Add";

}

public static void main(String a[]){

Test14 t = new Test14();

s = "New Instance";

String s = "Local";

method(s);

System.out.println(s);

System.out.println(t.s);

}

}

prints : Local

New Instance

I am unable to understand the output for (t.s) being New Instance.

Thanks !!

[516 byte] By [ginni78a] at [2007-10-1 1:41:40]
# 1

Strings are immutable. You pass in "New Instance" and your method takes that and makes a brand new String inside the method, "New InstanceAdd". But back in main, your s is still holding onto the "New Instance" String. If you want a mutable String-ish thing, look into StringBuffer (or StringBuilder in 1.5).

~Cheers

Adeodatusa at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 2
Name Strings something other than 's,' and try not to mix it up like you've done. If you name the Strings better, you'll probably fix your problem ;~)
Adeodatusa at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 3
Hi adeodatus, I am preparing for SCJP exam and while I was doing mock exams,I came across this question. I am not able to unserstand the output from System.out.println ( t.s )........New Instance.Thanks.
ginni78a at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 4

public class Test14{

static String s ="Instance";

public static void method(String s){

s+="Add"; // 4

}

public static void main(String a[]){

Test14 t = new Test14();

s = "New Instance"; // 1

String s = "Local"; // 2

method(s); // 3

System.out.println(s); // 5

System.out.println(t.s); //6

}

}

There are three different s-es.

A) The class variable s declared with static String s = "Instance"

B) The s that's a parameter to method()

C) The s that's local to main.

You need to understand that setting a reference variable just points that reference at an object. It doesn't change the contents of the object itself, and it doesn't affect andy other reference variables that might be pointing at the object.

You also need to understand that Java is a pass-by-value language. This means that the when you pass something as a parameter to a method, that thing's value gets copied, so the caller and the callee each have their own copy.

Finally, you need to understand that for reference types (variables that you use to access objects, like Strings), the value that gets copied and passed is basically the address of the object. It's as if I have your address written on a piece of paper, I write it on another paper, and hand it to somebody else.

1: s #A (the class variable) gets value "New Instance"

2: s #C (main's local) is declared and assigned value "Local". From this point on inside main, if we refer to just "s" (without t. in front) it will be s #C. This is because local variables "hide" member variables of the same name.

3: We call method, passing it s #C. What this really means is that we've told it the address of that String object. This means s #B (method's local) will be given the same value--the address of that String--as s #C. (Note that method's s #C "hides" the member variable s #A.)

4: We create a new String that's the concatenation of "Local" and "Add", and we stick that String's address in s #B. Note that this does NOT affect the original object that s #B was pointing at, and it doesn't affect any other variable that might be referring to either of those objects. It just erases what was written on that one piece of paper (which is separate from the other pieces of paper) and writes a new address there.

5: Back in main(), we print out s #C (remember, it "hides" the member variable #A). It's still pointing to the String "Local".

6: We print out s #A with the explicit t.s. It's still pointing to the "New Instance" that we assigned back in 1. (Remember, changing other variables to point at other objects doesn't affect what this particular variable points to.)

jverda at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 5
many many thanks.This was a gr8 explanation .
ginni78a at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 6

> Strings are immutable.

Just to be a pedantic ballsniffer...

String's immutability really has nothing to do with it. Even if String were mutable, the behavior wouldn't change due to the rules of hiding, reference assignment, and pass by value that I discussed.

The only way it would matter is if String's += were also redefined to modify that String object's contents, rather than reassigning the reference.

jverda at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 7
> many many thanks.This was a gr8 explanation .You're quite welcome. I'm glad it made sense. There were a lot of concepts in play there, and if they're all new to you, I could see where it would be overwhelming.
jverda at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 8

Let me list the code with line numbers included and then I'll do a line by line analysis and explain what's going on.

1. public class test{

2. static String s ="Instance";

3. public static void method(String s){

4. s+="Add";

5. }

6. public static void main(String a[]){

7. test t = new test();

8. s = "New Instance";

9. String s = "Local";

10. method(s);

11. System.out.println(s);

12. System.out.println(t.s);

1 3. }

14. }

Here we go. Usually, the program starts from method main but in our case since we have a static variable (also known as class variable) named 's', the control will start from line 2 where 's' is defined and at this time the value of s = Instance. That's because JVM will initialize static members before everything when it loads the class.

Next the control jumps to the main method on line 6. Nothing significant here. Then it moves to line 7 where it creates an object reference of the class type test. Then it moves to line 8 where it reassigns s a value of 'New Instance'.

Then it moves to line 9 where it creates a new variable named s which is instance variable and not the same as our first variable s which is a static one and still has the value of 'New Instance'. So currently we have 2 variables named s with appropriate values. Be sure to note/understand the difference between the two.

Then control goes to line 10 where we invoke a method call with an argument s. This argument s is the instace variable that holds the value 'local' at this time. You might ask how does the method know which s to use because currently we have two variables named s in the memory. One is static variable and the other one is instance variable. The method arguement would be the instance variable s because it is a local variable inside method main. The static variable is inside the class and is kind of hidden inside the local method because shadowing is occuring. Got it!

So then the control moves to line 3. Nothing significant here. Then control moves to line 4 and after execution of line 4 but before line 5 s holds the value 'LocalAdd'. This value will disappear as soon as we leave the method on line 5 because s was passed by value. In passed by value the method just gets a copy of the original variable. The original stays intact and the method manipulations don't affect the original.

So then the control moves to line 11 where s is the instance varialbe and holds the value 'local'. Then we move to line 12 where we are using an object referenc t to access class's static variable named s which currently holds the value 'New Instance'

Does it make sense now!! :)

Kasuri

kasuria at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 9
many many Thanks Kasuri for your time and explanation.You guys are amazingThanks again
ginni78a at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 10

> Then it moves to line 9 where it creates a new

> variable named s which is instance variable

No, it's not an instance variable. It's a method variable. Instance variables are declared outside of any method, and lack the static modifier.

> Then control goes to line 10 where we invoke a method

> call with an argument s. This argument s is the

> instace variable that holds the value 'local' at this

> time.

The method variable that points to the object "Local".

> You might ask how does the method know which s

> to use because currently we have two variables named

> s in the memory. One is static variable and the other

> one is instance variable.

Method variable, not instance variable.

> The method arguement would

> be the instance variable

method variable

> s because it is a local

> variable inside method main.

Yes, because method variables hide member variables.

> The static variable is

> inside the class and is kind of hidden inside the

> local method because shadowing is occuring. Got it!

This is actually hiding, not shadowing. The two are often used interchangeably, but the JLS does define them precisely, and they're not the same.

> So then the control moves to line 11 where s is the

> instance varialbe

method variable

jverda at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...
# 11

Jverd, thanks for catching my mistake. Indeed you're right that it's a local variable and not an instance variable. I realized that last night -- while sleeping as most of the time it happens! -- that a static method cannot access instance variables and so that made me realize that it was not an instance variable to begin with!!

I'm glad it least someone read my explanation!

Kasuri

kasuria at 2007-7-8 7:59:10 > top of Java-index,Security,Event Handling...