• HTML
  • CSS
  • JAVASCRIPT
  • JAVA
  • HTML
  • CSS
  • JAVASCRIPT
  • JAVA

What is Java Variable?

Syntax: data type variable [ = value][, variable [ = value] ...] ;

Variable in Java is a data container that stores the data values during Java program execution. Every variable is assigned data type which designates the type and quantity of value it can hold.

Variable is a memory location name of the data. The Java variables have mainly three types : Local, Instance and Static.

In order to use a variable in a program you to need to perform 2 steps:

  1. Variable Declaration
  2. Variable Initialization
Variable Declaration:

To declare a variable, you must specify the data type & give the variable a unique name.variable declaration

int  a, b, c ;

float pi;

double radius;

char a;

String name;

Variable Initialization:

To initialize a variable, you must assign it a valid value.

int  a=2, b=5 ,c=6;

float pi = 3.14f;

double area = 20.25d;

char a = ‘v’;

String name=”Java”;

Types of variables

In Java, there are three types of variables:

  1. Local Variables
  2. Instance Variables
  3. Static Variables
1) Local Variables

Local Variables are a variable that are declared inside the body of a method.

2) Instance Variables

Instance variables are defined without the STATIC keyword .They are defined Outside a method declaration. They are Object specific and are known as instance variables.

3) Static Variables

Static variables are initialized only once, at the start of the program execution. These variables should be initialized first, before the initialization of any instance variables.

Example: Types of Variables in Java

class Test{

     static int a = 10;     //static variable

     int b = 20;      //instace variable

     void method(){

            int c = 30;      // local variable

             }

}

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Course Content

  • JAVA
    • C++ vs Java
    • Features of Java
    • First Java Program
    • How to set Java path?
    • Internal Working of Java Program
    • Java Control Statements
    • Java Datatypes
    • Java Operators
    • Java Variables
    • JDK, JRE and JVM
    • Keyword
    • What is java

© 2025. ITTrainingWala