What will be the output of the following java Program ?
Explanation:
Within main z is assigned 6. z is printed. Output: 6
Within doStuff z is assigned 5. DoStuff2 locally sets z to 4 (but MyScope.z is set to 4), but in Dostuff z is still 5. z is printed. Output: 5
Again z is printed within main (with local z set to 6). Output: 6
Finally MyScope.z is printed. MyScope.z has been set to 4 within doStuff2(). Output: 4
2 comments:
When I used int z=4 in dostuff2(),I get 0 as output.why?
Hello Venkatesh, When you use int z=4; in doStuff2() method it will create a local variable named Z in doStuff2() body which is a local variable and can not be accessed out of doStuff2(), in this case the class level variable z still have default value 0.
Hope it helps.
Post a Comment