• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused with static code

 
anita dhar
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please explain me why the output is 3.And x-- should be 0.since it changes to 1 only in next place of x right ,as it is a post increment..Am i right ...Please explain this.

public class sample12 {

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;
}
}
 
anil kumar
Ranch Hand
Posts: 447
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Discussed Few Days before...

Check here


Thanks

Anil Kumar
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anita dhar

You might want to read this excellent article.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks it was really useful
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic