• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block understanding

 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vijitha Kumara
Bartender
Posts: 3909
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a question ?
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to write Question. Now i have updated Question Please explain how its output is 3
 
Akanksha Mittal
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that Akanksha
Its clear now.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic