Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Val's Exam Question numer 54

 
Jaffery Rab
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I need some help I do not understand the output for the following code
================================================
public class Question54 {
static int i;
int j;
static {
System.out.print("("+i+")");
i=3;
System.out.print("("+i+")");
}
{
System.out.print("("+i+","+j+")"); //line 1
j=2;
System.out.print("("+i+","+j+")");
}
Question54(){
System.out.print("("+i+","+j+")");
}
public static void main(String[] args) {
System.out.print("("+i+")");
Question54 q54 = new Question54();
System.out.print("("+q54.i+","+q54.j+")");
}
}
==================================================
I got the out put
(0)(3)(3)(3,0)(3,2)(3,2)(3,2)
but I think line 1 supposed to print (3,0)
I tried it like this
==================================================
public class hey{
int j;
{
System.out.println(j);
j=2;
System.out.println(j);
}
public static void main(String args[]){
new hey();
}
}
==================================================
and get the out put 0 2 as expected why is that?
Thanx
 
Paulo Silveira
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the question tabbed
 
Paulo Silveira
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right
it prints 3,0
like vals answer!
[ May 26, 2002: Message edited by: Paulo Silveira ]
 
Jaffery Rab
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got mixed up with order of execution....
Jaffery
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic