Mark As Completed Discussion

Variables

A variable in Java is a name that is associated with a value that can be changed.

Variables in Java refers to the name of the reserved area in memory. The variable name is a reference to the value stored in memory.

There are 3 types of variables in Java:

  1. Local variables
  2. Instance variables
  3. Static variables

Local variables

Local variables are declared in a method within a class. The scope is limited to the method which means you can't their values and access them outside of the method.

In this example, the instance variable and local variable have the same name. If I had not declared the instance variable and only declared the local variable then the print statement would have thrown an error. Even if they have the same name, you cannot change and access local variables outside the method.

JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment