It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static???????????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static????????????" Watch "Static????????????" New topic


Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
here we know that what changes happens in method does not affect the original value .THen why is it resulting the output as 3.
i thought as -1

Because the default value for x at ///1 is 0.
next x-- results in -1.///2
So System.out.println(x + y + ++x);will be (-1+0+0)

Please do explain me I didnot understand yet.
jyothi malapaka

Joined: Dec 21, 2005
Posts: 6
If we send the parameters i,j as arguments to myMethod() then, any changes in myMethod() would not be reflected in the main().

check this code....
public class Static
static{int x = 5;}
static int x,y;///1
public static void main(String args[])
System.out.println(x + y + ++x);}
public static void myMethod(int x,int y)
{y = x++ + ++x;}}

In this code any changes in x,y in myMethod() will still give the output as -1 because only the copy of x,y are sent to the method. These x,y in myMethod() are similar to local variables of the method.

But in your code, the static fields are used directly (u didnt send any duplicate of x,y) .So any change of x,y occuring in any method will be reflected at all the other methods.
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481

So You mean in the //////////////////////////ABC
if there are arguments like instead myMethod()
Then the changes in this method will affect the variables Right??
Which is seen in my pasted code
jyothi malapaka

Joined: Dec 21, 2005
Posts: 6
I meant ----The changes to variables sent to any method as arguments will not will not affect the original values because they point to two different memory locations.
for eg--
static x,y;
public static void main(String r[])
static void method(int a,int b)--->a=4;b=5;//////////////1
At /////1-->here x and a point to two different memory locations but the only similarity is that they have the same value. So changes to a are not reflected on x.

But in your code,x in myMethod() and x in main() both point to the same memory location.So changes to x in myMethod() were reflected on x in main().
[ December 24, 2005: Message edited by: jyothi malapaka ]
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425

Java is pass-by-value for parameters of your method, these are a copy of your value and therefore will not be affected outside the method when passed on to the method.

Furthermore, it doesn't matter if you pass on primitive types or object references; these are all copies when passed into methods.

BUT you have to understand that when you pass an object reference, which is a copy, then both references are pointing to the same object and therefore your method CAN change the object.

To support the last paragraph, have a look at the code below:

To support the last paragraph, have a look at te code below:

Try your free <a href="" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
x is static so its value will change even though you pass it as parameter

SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
Cheenu Subramanian
Ranch Hand

Joined: Aug 15, 2005
Posts: 40
Hi Jeff,
Though x is static, when x is passed as a parameter and is referred as x in that method, the changes will not reflect. Because u r actually accessing a local variable and not the static variable x.However if u access x as <classname>.x then it will be reflected.
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
Hi Cheenu

I am talking about the first post where x is never passed as parameter
so it always refers to the static variable x
santosh kothapalli

Joined: Dec 27, 2005
Posts: 26
public class Static{static{int x = 5;}static int x,y;///1public static void main(String args[]){x--;///2myMethod();System.out.println(x + y + ++x);}public static void myMethod(){y = x++ + ++x;}}

the first variable x in static block is never read,it has no significance.
The static variables are initialised first so x=0,y=0.
then in main block x is decremented by 1. so x=-1 and y=0;
when myMethod is called y=(1+-1) so y=0 and x=1;
when the expression is printed 1+0+2 which results in 3.

Santosh K<br />SCJP 1.4,SCWCD
I agree. Here's the link:
subject: Static????????????
jQuery in Action, 3rd edition