in reply to local variables and classes

Sounds like you need to read Coping with scoping; at least I think that's what you're asking. More to your actual question you'd want to check out Memoize.