DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on • Edited on

Instance(Object) Variable

What is an Instance Variable in Java?

  1. An instance variable is a variable that belongs to an object (instance) of a class.
  2. Every object gets its own copy of this variable.
  3. An instance variable is declared inside a class but outside methods.
  4. Its value is different for each object.

Where are instance variables stored in memory?

  1. Stored in Heap memory.
  2. Memory for these variables is allocated when a new object is created.
  3. Exists as long as object exists.

Does instance variables have default value?

  • Gets default values if not assigned.

Instance Variable Default value :

short = 0
byte = 0
int = 0
long = 0
float = 0.0
double = 0.0
char = '\u0000'
boolean = false
String = null

How to Acces this variable?

  • Through object using dot . operator. (Objectname.variablename)

Where it is declared?

  • Inside class and Outside methods.

What is another name for object variables?

  • Instance Variable.

Top comments (0)