File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program" Watch "Program" New topic


meena kumari

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;
public static void change_i(int i) {
i = 2;
i *= 2;

marc weber

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
meena kumari

Joined: Feb 01, 2006
Posts: 3
Thanks Marc
I agree. Here's the link:
subject: Program
jQuery in Action, 3rd edition