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

Static int

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was running this prg:
public class strtest{
private static int a;
public static void main(String args[]){

int k=modify (a);
System.out.println(a);
}

public static int modify(int a){
a++;
return a;
}
}

It prints 0.Shouldn't be the output 1.My thinking is that "a" is a static int.So it retains tha changed value.

Please help
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think about changing method modify with :

public static int modify(int i){
i++;
return i;
}

Remember : in Java methods, arguments are passed by value...
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi alpana,

The very basic thing you missed out probably. In the methods scope the value of 'a' will be that of the arguement 'a' passed to it, which will overshadow the class variable 'a'. If you change the parameter name for the method you will see the value of a as you have thought off.

public class StrTest {
private static int a;

public static void main(String args[]) {

int k = modify(a);
System.out.println(a);
}

public static int modify(int b) {
a++;
return a;
}
}

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My thinking is that "a" is a static int.So it retains tha changed value.



Why should/would a static variable retain its value ?
Do not confuse "static" with "final". Final will retain a variable's value, but not static.
Static means there is only 1 copy for all instances of the class. If there are 100 instances of your class, there will be only 1 instance of variable a. Of course, just like all other non-final variables, static variables are free to have their values changed.

Other things to note :
when a is passed into method "modify", a copy of a is passed (pass by value). The class wide static int a is not changed. That's why it remained as "0".

Seb Mathe's "in Java methods, arguments are passed by value..." is wrong. Primitives (eg int, double, char) are passed by value. Objects and arrays are passed by reference.



cheers,
Timothy
---
SCJP, SCBCD
[ September 30, 2005: Message edited by: Timothy Toh ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, to summarize, there are two variables named "a". Which one was modified?

Layne
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two varialbles with name 'a'
1. static int a
2. int a - local variable

you r incrementing the value of local variable 'a'

and printing the value of static variable 'a' which was not initialized by you and so it takes the default value 0
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic