-
January 19th, 2013, 04:33 PM
#1
running balance does not update - newbie question
Code:
public static void main(String[] args)
{
double runningBalance = 0;
blah(runningBalance);
System.out.println(runningBalance);
}
private static void blah(double runningBalance)
{
runningBalance += 8;
}
Hi guys,
Just a basic question on why running balance doesn't show 8 after I update it? Is this not allowed in Java? Apologize for such a newbie question, but I couldn't find examples to achieve what I want.
Thanks
-
January 19th, 2013, 09:15 PM
#2
Re: running balance does not update - newbie question
Java in general is pass by value instead of pass by reference. I believe this article will provide an excellent description of the issue.
But anyway, one possible alternative would be:
Code:
public class Test1 {
public static void main(String[] args)
{
double[] runningBalance = new double[1];
runningBalance[0] = 3.0;
blah(runningBalance);
System.out.println(runningBalance[0]);
}
private static void blah(double[] runningBalance)
{
runningBalance[0] = runningBalance[0] += 8.0;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|