Instance(Object) Variable
Source: Dev.to
What is an Instance Variable in Java?
- An instance variable is a variable that belongs to an object (instance) of a class.
- Every object gets its own copy of this variable.
- Declared inside a class but outside methods.
- Its value can differ for each object.
Where are instance variables stored in memory?
- Stored in heap memory.
- Memory for these variables is allocated when a new object is created.
- They exist as long as the object exists.
Do instance variables have default values?
If not explicitly assigned, instance variables receive default values:
short=0byte=0int=0long=0float=0.0double=0.0char='\u0000'boolean=falseString=null
How to access this variable?
Access instance variables through an object using the dot (.) operator:
ObjectName.variableName
Where is it declared?
Inside the class but outside any methods.
What is another name for object variables?
They are also called instance variables.