• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static variable question

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,
Following example's answer is 5 and 6.
public class StaticDemo
{
static int i = 5;
static
{
System.out.println("Static code: i = "+ i++ );
}
public static void main(String arg[])
{
System.out.println("main: i = "+ i++);
}
}
I thought the answer should be 6 and 6.
Please explain.
Thanks in advance.

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bob Vel:
Friends,
Following example's answer is 5 and 6.
public class StaticDemo
{
static int i = 5;
static
{
System.out.println("Static code: i = "+ i++ );
}
public static void main(String arg[])
{
System.out.println("main: i = "+ i++);
}
}
I thought the answer should be 6 and 6.
Please explain.
Thanks in advance.


Hi Bob,
The result shows that the post-increment operator (++ right after x) does it's job. What post-increment operator does is that it evaluates the associate expression with the current value of x first. Save the answer. Increment the variable. Then move the saving answer into the result's position.
In your case, two calls to System.out.println("..." + x++) will then yield 5 and 6 since:

The first call will just save the value of 5. Incerement x to 6. Send the value of 5 for answer.
The 2nd call will save the value of 6. Increment x to 7. Send the value of 6 for final answer.
I hope that helps....
Regards,
Lam
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The postfix operator when applied like i++, first uses the current value of i as the value of the expression first and then adds 1 to i. Here it first uses the value 5 to print then increases its value to 6. Similarly in the main.
To make it more clear you can add two more print statements in your code like this:
public class StaticDemo
{
static int i = 5;
static
{
System.out.println("Static code: i = "+ i++ );
System.out.println("After increment in static code i="+i);
}
public static void main(String arg[])
{
System.out.println("main: i = "+ i++);
System.out.println("After increment in main i="+i);
}
}
The output is:
Static code: i=5
After increment in static code i=6
main: i=6
After increment in main i=7
Hope this makes it clear
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic