As an argument variable, max will behave like a local variable. Meaning each recursion will have it's own copy of the variable max. And since you are not returning anything back, you will throw that value away, so by the end of the whole recursion the value of max in the first call into the function is still zero.
Consider making max a static variable within the function instead of passing it as an argument.
As an aside, your approach still has a problem that your function findDepth
doesn't "find" anything since it's not returning anything. You could also return max at each level but ignore it always, so only the caller into findDepth
actually deals with max. But this approach is gently nudging you back towards the original way to do it :).
Update
Asker had a follow up question.
But i want to point out to another article in stack overflow where they said the value will propagate down to the next recursive call.
As user430788 explained, max is a primitive type int
, passed by value, while the ArraList<Integer>
is not, so it will be passed by reference and will be shared by all instances of that function call.