Skip to content
Advertisement

What is the difference between local and instance variables in Java?

Except the scope and the storage differences, are there any other major difference between instance and local variables in Java?

Advertisement

Answer

One extra thing I can think of:

Instance variables are given default values, i.e., null if it’s an object reference, and 0 if it’s an int.

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).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement