Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Variable

It is a memory allocation, which we use, to store the data as per their data type.

Variables can be of some specific data type which contains some value according to the compatibility of their corresponding data type.

final keyword : When we declare a variable along with final keyword/modifier we can't change their value.

Syntax:

<data_type> <variable_name>;

Examples:

            
class Jtc{
public static void main(String arg[]){
              final int a = 10;
              System.out.println(a);
          //    a = 4000
               System.out.println(a);
         //       a = -56
              System.out.println(a);
      }
}
            
        
    10
    10
    10
            

In the above we are declaring a class Jtc and declaring a variable a and we store their value as 10 and further change the value to 4000 and again change the value to -56. It’s called variable because we can change its value. However, if we declare the variable with final keyword then we cannot change its value.

Types of Variables

Class Level Variable
Local variable

Class Level Variable

• When we declare a variable into a class directly, it is called class level variable.
• We can access a Class level variable from anywhere inside the Program.
• A class level variable gets its memory inside Heap.
• When we do not a initialize a class level variable, then JVM initialize class level variable by default value.

Class level variable is also of two types:

Instance variable
Static variable

Note: we will discuss the above further in tutorial of oops concept.

            
public class JtcClassLevelVariableExample {
    public static void main(String[] args) {
		Variable vl = new Variable();
		vl.variable_m2();
    }
}

class Variable {
	// class level variables
	byte b;
	short s;
	int i;
	long l;
	float f;
	double d;
	boolean bl;
	char c;

	void variable_m2() {
		System.out.println("byte : " + b);
		System.out.println("short : " + s);
		System.out.println("int : " + i);
		System.out.println("long : " + l);
		System.out.println("float : " + f);
		System.out.println("double : " + d);
		System.out.println("boolean : " + bl);
		System.out.println("char : " + c);
	}

}
            
        
    byte : 0
    short : 0
    int : 0
    long : 0
    float : 0.0
    double : 0.0
    boolean : false
    char :
            

Here, we are defining a class with name variable and inside a class we declare class level variables and here we are also creating one method variable_m2() and here I’m printing variable default value and after declaring the method we are creating Object in this class and then calling this method and printing this.

Local variable:

• Any variable that is defined inside any block, any method, any constructor or any local context is called local variable.
• we must have to initialize the local variable before their use
• Local variable memory will be allocated only when that corresponding context is being processed and immediately after processing the context that memory will be destroyed.
• You can keep same name of localvariable and class name variable but when you are accessing that local variable in that context itself then that local variable only will be accessed.

            
package com.jtcindia.org;

class LocalVariable {
  int a = 10; // class level variable of A class

  void localVariable_m1() {
  int b = 20; // local variable 
  System.out.println("--localVariable_m1() in LocalVariable class--");
  System.out.println(b);
  }

  void localVariable_m2() {
  int c = 30; // local variable 
  System.out.println("--localVariable_m2() in LocalVariable class--");
  System.out.println(c);
  }

  void localVariable_m3() {
  int d = 40; // local variable 
  System.out.println("--localVariable_m3() in LocalVariable class--");
  System.out.println(d);
	}
}

public class Jtc {
	public static void main(String[] args) {
		System.out.println(new LocalVariable().a);
		new LocalVariable().localVariable_m1();
		new LocalVariable().localVariable_m2();
		new LocalVariable().localVariable_m3();

	}
}

            
        
    10
    --localVariable_m1() in LocalVariable class--
    20
    --localVariable_m2() in LocalVariable class--
    30
    --localVariable_m3() in LocalVariable class--
    40
            

Here, we are defining one class LocalVariable and inside this class we are creating methods and inside this method we are declaring local variable. And if we want to access this local variable, we will have to create object of LocalVariable class for calling this method for accessing this value.