Local variable AND Instance variable / 局部變量 與 實例變量區別

One extra thing I can think of:ide

Instance variables are given default values, ie null if it's an object reference, 0 if it's an int.this

Local variables don't get default values, and therefore need to be explicitly initialized (and the compiler usually complains if you fail to do this).code

 

Local variable:ci

  • is declared inside a method/constructor or within a block (enclosed in braces)
  • must be initialized before use, otherwise it won't compile.

Instance variable:get

  • is declared inside a class.
  • initialization is not compulsory: if omitted, it contains default value (0, 0.0, false, null, etc.)
相關文章
相關標籤/搜索