running balance does not update - newbie question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: running balance does not update - newbie question

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    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

  2. #2
    Join Date
    Apr 2004
    Posts
    99

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center