Java Instance Variables vs Local Variables

后端 未结 10 2083
庸人自扰
庸人自扰 2020-11-30 01:11

I\'m in my first programming class in high school. We\'re doing our end of the first semester project. This project only involves one class, but many methods. My question is

10条回答
  •  失恋的感觉
    2020-11-30 01:57

    Local variables internal to methods are always prefered, since you want to keep each variable's scope as small as possible. But if more than one method needs to access a variable, then it's going to have to be an instance variable.

    Local variables are more like intermediate values used to reach a result or compute something on the fly. Instance variables are more like attributes of a class, like your age or name.

提交回复
热议问题