How does that work because if the product doesnt exist found wont be initialised? Which leads me to my next question - what protection should i add in to stop the program from throwing an error if its not initialised?

For the first variant you have to add a 'return null;' statement at the end of your method body, indicating that the wanted Product was not in the Collection; or you have to initialize local variable 'found' to null to keep the compiler's mouth shut. It doesn't matter which of the two variants you use (I prefer the first variant because it doesn't need a local variable but that's just me ;-)

kind regards,

Jos

I have the stamina of a seal; I lie on the beach instead of running on it.

The problem is this: what happens if you have no Products in "this"? Or if the pid is not found in any of the Products?

In that case, your "found" variable will not be initialized to anything. Java doesn't know what to do with that. What would you like to return in that case?

Edit- Too slow. But I can almost guarantee that "adding a public variable" is not at all what you should be doing. All that's doing is initializing it to null by default. Class variables are automatically initialized to a default value (in the case of Objects, it's null). Local variables are not.

I think his variant was getting rid of the local, unless I misread his post.

You misread his post. Adding a global variable only worked because it was automatically initialized to null. There's really no point to doing that instead of simply initializing the local variable to null or returning null at the end of the method.