Hello everyone,
The names free_spirit and am a new registered user on this site. I am also a new programmer to the Java language. There is a problem in a book I am learning from, Introduction to Java Programming and Data Structures by Daniel Liang (11th edition), that I'm having trouble with.
public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i = 0; i < 100; i++) increment(myCount, times); System.out.println("count is " + myCount.count); System.out.println("times is " + times); } public static void increment(Count c, int times) { c.count++; times++; } } class Count { public int count; public Count(int c) { count = c; } public Count() { count = 1; } }
The output is:
count 101
times 0.
I understand why the output for test is zero but not so much for count. It would be helpful if anyone can explain. Thank you!


LinkBack URL
About LinkBacks
Reply With Quote