What is a Variable?
A variable is a named memory location used to store a value.
That value can be changed during the program execution.
Syntax:
dataType variableName = value ;
Enter fullscreen mode
Exit fullscreen mode
Ex:
int age = 10 ; String animal = "Tiger" ;
Enter fullscreen mode
Exit fullscreen mode
Here , age and animal are variables that contains some value.
Types of Variables:
There are mainly three types of variables:
- Local variable - Instance variable - Static variable
Enter fullscreen mode
Exit fullscreen mode
Local Variable:
Variable which is declared inside a method , constructor or block is called Local Variable .
You must initialize the value for local variable, before accessing it.
These local variables can be accessed within that block only.
Local Variables don't have Default values.
ex:
class Animal {
public void Dog () { int age = 5 ; //Local Variable String name = "Puppy" ; //Local Variable System . out .…