wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program" Watch "Program" New topic
Author

Program

meena kumari
Greenhorn

Joined: Feb 01, 2006
Posts: 3
Hi All,
I am new to java .I am finding difficulty in understanding the below program flow.
Can any one explain me how the program works.



public class example {
int i = 0;
public static void main(String args[]) {
int i = 1;
change_i(i);
System.out.println(i);
}
public static void change_i(int i) {
i = 2;
i *= 2;
}
}


Thanks,
Meena
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

When arguments are passed to a method, they are copies of the original value. And these copies are local to the method scope.

In this case, there is an int i defined in the class, another int i defined in the main method, and a third int i that's local to the change_i method. So when the value of i is changed within the change_i method, it's only that local value that changes. The int i within main is still 1, and the int i defined at the class level is still 0.

Compile and run the code below, which I've modified to output each of these different variables...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
meena kumari
Greenhorn

Joined: Feb 01, 2006
Posts: 3
Thanks Marc
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Program