unwanted reinitialization of variables during recursive calls

Hi,

i have this recursive knapsack algorithm. The variables (maxValue, spaceRemaining, currentValue) do not seem(?) to be reinitialized -seems strange. Can somebody point out why?

What if i instead of having

int maxValue=0, spaceRemaining=0, currentValue=0;

,

i had

int maxValue, spaceRemaining, currentValue;

maxValue=0;

spaceRemaining=0;

currentValue=0;

,

publicstaticint pack(int capacity)

{

int maxValue=0, spaceRemaining=0, currentValue=0;

for (int i=0; i<svs_.size(); i++)

{

spaceRemaining=capacity-svs_.getSpace(i);

if (spaceRemaining>=0)

{

currentValue=pack(spaceRemaining)+svs_.getValue(i);

}

if (currentValue > maxValue)

{

maxValue=currentValue;

}

}

return maxValue;

}//pack

[1504 byte] By [uiga] at [2007-11-27 3:29:03]
# 1
I don't understand "do not seem(?) to be reinitialized" in relation to the thread title.
sabre150a at 2007-7-12 8:31:57 > top of Java-index,Java Essentials,Java Programming...
# 2

"Unwanted" is not necessary in the title. I just imply, that i do not want the variables to be initialized during the recursive calls. This is what happens in this case, but with this implementation, i would expect things to go wrong. Why don' they go wrong? That's another way to ask my question

uiga at 2007-7-12 8:31:57 > top of Java-index,Java Essentials,Java Programming...
# 3
Then make the variable instance variables, not method variables.
sabre150a at 2007-7-12 8:31:57 > top of Java-index,Java Essentials,Java Programming...