What are instance and local variables?

  • Instance variable or class variables are the same thing are created inside a class and outside the method and can be accessed by everymethod

  • Whereas local variables are created inside a block such as, inside method or constructor and they can be only accessed inside that block

What are the default values assigned to variables and instance in java ?

  • For instance the default values are assigned by the default constructor

  • For int -> 0

  • For boolean -> false

  • For ref -> null

  • char: ‘\u0000’ (null character

  • String is assigned as null in case of instance variable

  • A compiler will never assign a default value to local variable

  • A compiler will assign the default values only for the instance variable

Example