• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why this with static

 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class GFC404 {
private static int x=1;
static void m1(int x, int y) {x++; y++;}
public static void main (String[] args) {
int y=3; m1(x, y);
System.out.println(x + "," + y);
}}


the value of x should be 2 after invocation of method but it shows 1??
static variable are class variables and each instance points to that same variable then why no increment in x???

 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikky.ag agrawal wrote:
the value of x should be 2 after invocation of method but it shows 1??
static variable are class variables and each instance points to that same variable then why no increment in x???


Hi Vikky,
we have here variables of the primitive data type int. So nothing points to anywhere. Actually for this problem it doesn't matter if we have instance or static variables.
After the call to the method, copies of the variables are creates and these copies are incremented and not the static variable x or the local variable y:
 
Kieren Dixon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass by reference VS Pass by value
 
wei luo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, man,

if you look at the code, the m1 method is not increasing the "private static int" x at all!!!
m1 is merely increasing a copy of one of the parameters that is passed to it.
The question is set up in this way to fool us, actually, the following code behaves the same way as your original code:

 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please site your sources when posting code
 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Wheeler wrote:
vikky.ag agrawal wrote:
the value of x should be 2 after invocation of method but it shows 1??
static variable are class variables and each instance points to that same variable then why no increment in x???


Hi Vikky,
we have here variables of the primitive data type int. So nothing points to anywhere. Actually for this problem it doesn't matter if we have instance or static variables.
After the call to the method, copies of the variables are creates and these copies are incremented and not the static variable x or the local variable y:


yeah i got the idea actually i was confused...
thanks for your efforts..
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikky.ag agrawal wrote:
Bob Wheeler wrote:
yeah i got the idea actually i was confused...
thanks for your efforts..

You are welcome. But PLEASE site your source. Otherwise this post will be deleted, I guess. And other people won't benefit from my superior answer
cheers
Bob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic