wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP Mock Question : static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP Mock Question : static" Watch "SCJP Mock Question : static" New topic
Author

SCJP Mock Question : static

Mainak Goswami
Ranch Hand

Joined: May 21, 2006
Posts: 50

Hi,

Please find below the code:
public class Static {
static{
int x=5;
}
static int x,y;
public static void main(String args[]){
x--;
myMethod();
System.out.println(x+y+ ++x);

}
public static void myMethod(){
y=x++ + ++x;
}

}
Output is 3.
Can anyone please explain me the flow and why the output is 3?
Actually according to me the static variables x and Y are initialized to 0.After that when the static block runs the value of x is modified to 5.
As per the flow goes the answer should be 15.
Thanks in advance.


------------------------------------
Mainak
idiotechie
------------------------------------
Gaurav Bhatia
Ranch Hand

Joined: Jan 01, 2007
Posts: 49
The static block is declaring a new local variable (scope : within static block) and initializing it to 5.
The static variable 'x' is still 0 (after static block execution).

So, using that value of x if you calculate the result will be printed as 3.

Hope this clarifies.


~Gaurav<br />SCJP5
Umesh Patil
Greenhorn

Joined: Oct 08, 2007
Posts: 3
Hi ,

This is my first reply on this site, Sorry if i have done some mistake. please check the code.


public class Static {
static{
int x=5;// Define in static block But we are not setting value to static class member.
}
static int x,y;// Initial values x=0 y=0
public static void main(String args[]){
x--;// x=-1 now
myMethod();// Here x=1 and y=0
System.out.println(x+y+ ++x);// 1+0+(2) == 4
}
public static void myMethod(){
y=x++ + ++x;////y= -1 + 1 = 0

}
}
Mainak Goswami
Ranch Hand

Joined: May 21, 2006
Posts: 50

Many thanks to Gaurav and Umesh. My doubt is clear now.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Originally posted by Umesh Patil:
Hi ,

This is my first reply on this site, Sorry if i have done some mistake. please check the code.


public class Static {
static{
int x=5;// Define in static block But we are not setting value to static class member.
}
static int x,y;// Initial values x=0 y=0
public static void main(String args[]){
x--;// x=-1 now
myMethod();// Here x=1 and y=0
System.out.println(x+y+ ++x);// 1+0+(2) == 4
}
public static void myMethod(){
y=x++ + ++x;////y= -1 + 1 = 0

}
}


Dont you mean 3 as the answer?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: SCJP Mock Question : static
 
Similar Threads
Doubt from Whizlab Question
static block understanding
confused with static code
Static????????????
Please help