Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static int

 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • 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...
 
Pradeep Kadambar
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • 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;
}
}

 
Timothy Toe
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • 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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, to summarize, there are two variables named "a". Which one was modified?

Layne
 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic