The bond forever!

View previous topic View next topic Go down

Java The bond forever!

Post by BIT0102-Mohaimin on Sat Feb 12, 2011 8:17 pm

Here is a little java code.

Code:

public class WhatIsHappening {
   private String value;
   
   public WhatIsHappening() {
      setValue("Nothing");
   }
   
   public void setValue(String value) {
      this.value = value;
   }
   
   public void print() {
      System.out.println(value);
   }
}

I assure that the class does not have any trick. Here is the main method. Check it out carefully.
Code:

public static void main(String[] args) {
      WhatIsHappening a = new WhatIsHappening();
      WhatIsHappening b = new WhatIsHappening();
      
      a.setValue("A1");
      b.setValue("B2");      
      a.print();
      b.print();
      
      a = b;
      
      a.setValue("A2");
      b.setValue("B2");      
      a.print();
      b.print();
      
      b.setValue("B3");
      a.setValue("A3");
      a.print();
      b.print();
   }

Here is the output
Code:

A1
B2
B2
B2
A3
A3

Now explain the output.


Last edited by BIT0102-Mohaimin on Sat Feb 12, 2011 8:23 pm; edited 1 time in total (Reason for editing : Improper use of tags)
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum