This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why this with static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why this with static" Watch "why this with static" New topic
Author

why this with static

vikky agrawal
Ranch Hand

Joined: Dec 18, 2008
Posts: 65
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???


scjp 6
http://algorithmsea.blogspot.com
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
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:


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Kieren Dixon
Greenhorn

Joined: Jan 30, 2009
Posts: 27
Pass by reference VS Pass by value
wei luo
Greenhorn

Joined: Jul 17, 2008
Posts: 14
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

Joined: Nov 07, 2008
Posts: 488
Please site your sources when posting code

SCJA
~Currently preparing for SCJP6
vikky agrawal
Ranch Hand

Joined: Dec 18, 2008
Posts: 65
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

Joined: Apr 24, 2009
Posts: 317
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
 
 
subject: why this with static
 
Similar Threads
static variable
Wrapper instances question
David Smalley
primitive value
Dan's questions : static variable