• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

static block understanding

 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a question ?
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to write Question. Now i have updated Question Please explain how its output is 3
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that Akanksha
Its clear now.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic