• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operator and Assignment Mock Question

 
R .sourav nayak
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
could someone explain me how the output of the following program is 2020

class Base{
public static int iAge;

}
public class CSturt{
public static void main(String argv[]){
CSturt cs = new CSturt();
cs.ww();
}
public void ww(){
Base b = new Base();
Base b2 = new Base();
b.iAge = 10;
b2.iAge = 20;
System.out.print(b.iAge);
Integer iw = new Integer(b.iAge);
col(iw);


}
public static void col(Object o){
Integer iw = (Integer) o;
System.out.print(iw.intValue());
}
}



thanks in advance.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that iAge is a static variable.

So even though you have two instances of the class, using either instance to access iAge accesses the same variable.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saurav,

You may try the following code:

 
Srinivas Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely a good question with a trap ;-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic