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
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;
}
}