Java member variables and local variables difference

Members and local variables difference

Variable depending on the location of the definition, we give the variables played a different name. As shown below:

the difference 

Different position in the class (focus)

  • Member variables: class, method outside
  • Local variables: the process or method declaration (formal parameter)

Scope is not the same (focus)

  • Member variables: class
  • Local variables: the method

Different initialization values ​​(focus)

  • Member variable: there are defaults
  • Local variables: no default. You must define, assign, and finally use

Different locations in memory

  • Member variables: heap memory
  • Local variables: Stack Memory

Different life cycle

  • Member variables: With the creation of the object exists, the object disappear with the disappearance
  • Local variables: With call a method exists, along with the method call is completed disappear

Guess you like

Origin www.cnblogs.com/wurengen/p/11922365.html