• 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

plz explain this code

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of trying to compile and run the following code?
(Select one correct answer)
--------------------------------------------------------------------------------

public class Test026
{
static Test026 t = new Test026();
String str;

public static void main(String args[]) {
Test026 t = new Test026();
t.method("0");
}

void method(String str) {
str = str;
System.out.println(str);
System.out.println(t.str);
System.out.println(this.str);
}
}

--------------------------------------------------------------------------------

A: The output is:
0
null
null
B: The output is:
0
0
0
C: The output is:
0
0
null
D: The output is:
0
null
0

This is the code from www.jiris.com scjp-2 test. I am not understanding this code. plz explain this code anybody. Ans is A
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two instances of class Test026 created when this program is run. One is created when the class itself is loaded. The static variable Test026.t refers to this first instance.

The second instance of this class Test026 is created when the main method runs. The local variable t in main points to this second instance. The method method is called through this second instance.

So imagine we are in the method method and our context is the second instance. Our method's parameter str is passed the argument "0". We do str = str which effectively does nothing because str is our local parameter variable. Our str still points to "0". We print that, so the first thing output is 0.
The next println outputs t.str. t refers to the static variable Test026.t in our class which refers to the first instance of Test026 that was created. The variable str in that instance still has its default value of null. So null gets output.
The next thing we do is to print this.str. But that is our object's str. Nothing set that either so it's still null. So we output null.

In conclusion 0,null,null will be printed.
To solve this type of problem draw some boxes representing the instances created and put yourself inside those objects. Don't forget to represent the class object which has the static variables.
[ October 10, 2004: Message edited by: Barry Gaunt ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic