Java Variables
Variables are data value storage containers.
Syntax:
datatype variableName = value
Create Variables
To declare a variable, you need to mention its type and give it a value.
int number
=
50;
Types of Variables in Java
There are three types of variables in java:
- Local Variables
- Static Variables
Local Variables
A variable that has been declared within a block, method, or constructor is called a local variable.
Example:
int number
=
50;
Static Variables
Static variables are declared using the static keyword inside a class but outside any method, constructor, or block.
Example:
public class
Main
{
public static
String
name;
public static void main(String[] args) {
name =
" John";
System.out.println("Hello"
+ name);
}
}
Example:
Hello John