Java Instance Variables vs Local Variables

后端 未结 10 2081
庸人自扰
庸人自扰 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:55

    Short story: if and only if a variable needs to be accessed by more than one method(or outside of the class) create it as an instance variables. If you need it only locally, in a single method, it has to be a local variable. Instance variables are more costly than local variables.
    Keep in mind: instance variables are initialized to default values while local variables are not.

提交回复
热议问题