Lecture 3.1 - Variable Types
Lecture 3.1 - Variable Types
Local Variables
Class Variables
Instance Variables
Variable Types
Different Types of Variables
Local Variables
• Variables declared inside the scope of any method, constructor, loop, conditional statements
are known as local variables.
• Local variables do not have any existence outside the scope it is declared.
• The concept of default value is not applicable for local variables.
Example A list of local variables declared
in the example is:
public void changeValues(int a){ a and x is accessible/exists
int x; • int a
• int x throughout the whole
for(int i=0; i<5; i++){ method.
int y = i + a; • int i i and y is accessible/exists
if(y%2 ==0){ • int y
only within the for loop.
int z = a * y; • int z z is only accessible/exists
} These variables do not have any inside the if block.
} existence outside the block they
} were declared.
Variable Types
Different Types of Variables
Class Variables
• Variables declared inside the scope of a class but outside the scope of any method
or constructor are known as Class Variables if and only if the keyword static is
used in the declaration of the variable.
• It can be accessed from anywhere inside the class it is declared.
• It can also be accessed from outside of the class it is declared depending its access
modifiers.
• We do not need any object to access it. It is accessed using the class name.
• All the objects of a class, share the same memory for a class variable.
Instance Variables
• Variables declared inside the scope of a class but outside the scope of any method
or constructor are known as Class Variables if and only if the keyword static is not
used in the declaration of the variable.
• It can be accessed from anywhere inside the class it is declared.
• It can also be accessed from outside of the class it is declared depending its access
modifiers.
• We have to use an object to access it.
• Each of the objects of a class, hold different memory for an instance variable.