Skip to main content

Java Variables and Variables Types

A variable is basically a container that holds the value for the java program to be executed. Normally a variable is declared along with its respective datatype of the value.

A variable is also a memory location, a type of reserved memory which means that it can also be varied and can be changed.

Example:

String sorcerer = "Doctor Strange";

Here sorcerer is the variable that comes with string datatype which holds the string value.

Three of the variables play role in this.

  1. Local Variable
  2. Static Variable
  3. Instance Variable

1. Local Variable

Any variable that is declared within a method is called a local variable. The particular variable cannot be used out of that specific method or anywhere else within the class. No other methods within the class would be aware of the declared local variable.

public class Testing
{
    private void varDec()
    {
        int x = 007; //Local Variable
        int y = 10;
        int z = x + y;

        System.out.println(z);
    }
    
    public static void main(String[] args)
    {
        Testing t = new Testing();
        t.varDec();
    }
}

Output: 17

2. Instance Variable

Instance variables are declared inside the class and outside the body of the method. It is instance-specific and not shared among other instances. Access modifiers can be given for declaring the instance variable.

public class Testing
{
    public String agentName; //Instance Variable
    public int id; //Instance Variable

    public Testing(String name)
    {
        agentName = name;
    }
    
    private void id(int num)
    {
        id = num;
    }

    private void printAgent() 
    {
        System.out.println("Agent Name : " +agentName);
        System.out.println("id : " +id);
    }

    public static void main(String[] args)
    {
        Testing t = new Testing("James Bond");
        t.id(007);
        t.printAgent();
    }
}

Output:

Agent Name : James Bond
id : 7

3. Static Variable

Class variables are said to be known as static variables. Mostly declared with a static keyword outside the method block or constructor.

  • Static variables are values that cannot be modified and that remain static.
  • Thus static variables cannot be local.
  • Memory allocation for this can happen only once when the class is loaded in the memory.
  • It can be made a single copy and can be shared among the other instances of the class.
public class Testing
{
    static String name;
    static int id;
    
    static
    {
        name = "James Bond";
        id = 007;
    }

    public static void main(String[] args)
    {
        System.out.println(name);
        System.out.println(id);
    }
}

Output:

James Bond
7
Tags
Submitted by arilio666 on October 7, 2021

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.