• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Static?

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[])
{
x--;///2
myMethod(x,y);
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
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[])
{
x=4;y=5;
method(x,y);
}
static void method(int a,int b)--->a=4;b=5;//////////////1
{
a=9;
b=5;
}
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 ]
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karu,

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:

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x is static so its value will change even though you pass it as parameter
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic