0
0
Javaprogramming~5 mins

Instance variables in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an instance variable in Java?
An instance variable is a variable declared inside a class but outside any method, constructor, or block. Each object of the class has its own copy of the instance variable.
Click to reveal answer
intermediate
Where are instance variables stored in memory?
Instance variables are stored in the heap memory as part of the object they belong to.
Click to reveal answer
beginner
How do instance variables differ from static variables?
Instance variables belong to objects and each object has its own copy. Static variables belong to the class itself and are shared among all objects.
Click to reveal answer
beginner
Can instance variables have default values if not initialized?
Yes, instance variables have default values based on their type (e.g., 0 for int, null for objects) if not explicitly initialized.
Click to reveal answer
beginner
Example: Identify the instance variable in this code snippet:<br><pre>class Car {<br>  String color;<br>  void setColor(String c) {<br>    color = c;<br>  }<br>}</pre>
The instance variable is <code>color</code>. It is declared inside the class but outside any method, so each Car object has its own color.
Click to reveal answer
What does each object of a class have regarding instance variables?
AIts own copy of instance variables
BShares instance variables with other objects
CNo instance variables
DOnly static variables
Where are instance variables declared in Java?
AInside methods
BInside the class but outside methods
COutside the class
DInside constructors only
What is the default value of an uninitialized instance variable of type int?
Aundefined
Bnull
C0
D1
Which memory area stores instance variables?
AHeap
BStack
CMethod area
DRegister
How do instance variables differ from static variables?
AInstance variables are shared; static are unique
BBoth are the same
CStatic variables are stored in heap; instance in stack
DInstance variables belong to objects; static belong to class
Explain what instance variables are and how they behave in Java objects.
Think about how each object keeps its own data separate.
You got /4 concepts.
    Describe the difference between instance variables and static variables in Java.
    Consider who owns the variable: the object or the class.
    You got /4 concepts.