DEV Community

Cover image for Variables in Java
Kavitha
Kavitha

Posted on

Variables in Java

Variables
A variable is a container used to store data values. The value of a variable can change while the program is running.

Syntax:

dataType variableName = value;

Enter fullscreen mode Exit fullscreen mode

Example:

int number = 5;

Enter fullscreen mode Exit fullscreen mode

Types of Variables

  • Local variables are declared inside methods and used only within them.
  • Instance variables are declared inside a class but outside methods.
  • Static variables are shared among all objects of a class.

Example:

class Test {
    static int count = 10;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)