aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static block understanding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static block understanding" Watch "static block understanding" New topic
Author

static block understanding

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

public class test {

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;

}

}

Please explain how its output is 3?


thanks
Tanu Gulati
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Do you have a question ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

I forgot to write Question. Now i have updated Question Please explain how its output is 3
Akanksha Mittal
Greenhorn

Joined: Jul 29, 2009
Posts: 26
Hi Tanu

Please Use Code Tags, makes easier to understand and explain.



Inside the static block you have x declared, so that makes it local to that block and so not used after that.

Now since x and y are class variables, so they are initialized to 0.
while using x and y in the main method only the x and y having initial values 0 are used and hence

after mymethod(): y=0 & x=1
and in println its (1+ 0 + 2)

Remember Postincrement operator increments the value only after the execution of the current statement (in this case - y = x++ + ++x; )
whereas pre increment modifies the variable before its usage
Sunny X Narula
Greenhorn

Joined: Dec 07, 2008
Posts: 22
One doubt
Remember Postincrement operator increments the value only after the execution of the current statement (in this case - y = x++ + ++x; )


Before the statement, x is -1 and y is 0.
So as per the explanation above the code must translate to:


y should not get the value from x++ (thats what I thought) but it does.
In fact it behaves as if the code was


That is the only way y can end up with 1 else it should be -1.

May be I am missing some concept here/I might be confused but that is how it works.
I am guessing this has something to do with priority while evaluating an expression. I think ++ has higher priority than +.
Any references will be helpful.
Thanks in advance for any help.
Akanksha Mittal
Greenhorn

Joined: Jul 29, 2009
Posts: 26
Whenever you say


This will make x=1 & y=0

Similarly,


will make x=1 & y=1


In the scenario mentioned above, Pre Increment will be evaluated first, and post increment evaluated last based on precedence so the expression becomes


Therefore y=0, x=1 in the method "myMethod"
Sunny X Narula
Greenhorn

Joined: Dec 07, 2008
Posts: 22
Thanks for that Akanksha
Its clear now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static block understanding