wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Val's Exam Question numer 54 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Val Watch "Val New topic
Author

Val's Exam Question numer 54

Jaffery Rab
Ranch Hand

Joined: Jan 18, 2002
Posts: 36
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

Joined: May 21, 2002
Posts: 32
here is the question tabbed
Paulo Silveira
Ranch Hand

Joined: May 21, 2002
Posts: 32
you are right
it prints 3,0
like vals answer!
[ May 26, 2002: Message edited by: Paulo Silveira ]
Jaffery Rab
Ranch Hand

Joined: Jan 18, 2002
Posts: 36
I think I got mixed up with order of execution....
Jaffery
 
 
subject: Val's Exam Question numer 54
 
Similar Threads
A tough one - on flow control
ExamLab PracticeTest-1 Q54
Doubt with static block+variable
static block,non-static block, & constructor issue
Marcus Exam#2 Q54