Java Variables

In this page, we will learn about Java Variables, What is Java Variables?,What is Local Variable?, What is Instance Variable?, What is Static Variable?, Local Variable Example, Instance Variable Example, Static Variable Example.


What is Java Variables?

A Variable is a container which clasp the value while the Java program is executed. A variable is allocated with a data type. Variable is a name of a memory location. There are three types of variables in java, these are:

  • Local
  • Instance
  • Static
Variables In Java

Variable:-

Variable is name of reserved space allotted in memory. In alternative words, it’s a reputation of memory location. It’s a mix of “vary + able” meaning its worth can be modified.

Variables In Java

Example:


 import java.io.*;
  Class Simple{  
    Public static void main(String args[]){  
    int x = 20; // x is a variable
    }  
  }  


What is Local Variable?

A variable declared within the body of the strategy is named native variable. You’ll use this variable solely at intervals that technique and also the different strategies within the category are not even aware that the variable exists.

[N:B: A local variable can not be outlined with “static” keyword.]

  • These variable are created when the block is entered or that function is called and destroyed after finishing the task or when the call returns from the function.
  • The scope of these variables exists only within the function or block in which the variable is declared. i.e. You can access these variable only within that block or function.
  • Initialization of Local Variable is the must.

Local Variable Example :-


  import java.io.*;
  public class A {
    public void TeacherAge(){
      // local variable age
      int age = 25;
      System.out.println("a = " + age);
    }

    public static void main(String args[]){
      Teacher obj = new TeacherAge();
      obj.TeacherAge();
    }
  }

Output:


  a = 25


What is Instance Variable?

A variable declared within the category however outside the body of the tactic, is named instance variable. It’s not declared as static.

It is referred to as instance variable as a result of its worth is instance specific and isn’t shared among instances.

  • As instance variables are declared within a class, these variables are created when an object of the class is created as wel as destroyed when the object is destroyed.
  • Unlike local variables, you may use access specifiers for instance variables. If you don't specify any access specifier then the default access specifier will be used.
  • Initialization of Instance Variable isn't Mandatory. By default Its value is 0
  • Instance Variable can be accessed only by creating its object.

Instance Variable Example :-


  import java.io.*;
  class Marks {
    int banMarks;    
  }
  class MarksDemo {
    public static void main(String args[]){
      // first object
      Marks obj1 = new Marks();
      obj1.engMarks = 50;
      System.out.println("Marks = " + obj1.banMarks);
    }
  }

Output:


  Marks = 50


What is Static Variable?

A variable which is declared as static is termed static variable. It cannot be native. You can produce a single copy of static variable and share among all the instances of the category . Memory allocation for static variable happens just once when the category is loaded in the memory.

  • These variables are declared is same as instance variables, the main difference is that static variables are declared using the static keyword within a class outside any method, constructor, or a block .
  • Unlike instance variables, you can only have one copy of a static variable per class irrespective of how many objects you create.
  • Static variables are created at the beginning of program execution and destroyed automatically when its execution ends.
  • Initialization is not Mandatory for Static Variable. By default its value is 0.
  • If you try to access the static variable as like as Instance variable (through an object), the compiler will show the warning message and it will not halt the program. The compiler will replace the object name to the class name automatically.
  • If you try to access the static variable without the class name, Compiler will automatically add the class name.

To access static variables, you don't need to create an object of that class, you can simply access the variable as class_name.variable_name;.

Static Variable Example :-


  import java.io.*;
  class Emp {
    public static double salary;
    public static String name = "Tuhin";
  }

  public class EmpDemo {
    public static void main(String args[]){
      Emp.salary = 1000;
      System.out.println(Emp.name);
      System.out.println(Emp.salary);
    }
  }

Output:


  Tuhin
  1000