• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

why this with static

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pass by reference VS Pass by value
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please site your sources when posting code
 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic