Variable in Java

Variable in java is nothing but the container that stores the value of data during execution of the program. For example, if you write int abc=100; then here  abc is a variable which is of type int and holding value as 100.

Declare variable in java:

data_type variable_name = variable_value;

Here variable_value is optional, you can assign value to variable_name later also. For example int abc;

Variable naming convention in java:

  1. Variable name cannot contain empty spaces. For example, String employee name=”Ajit”;  you should write it as String employee_name=”Ajit”; or String employeeName=”Ajit”;
  2. Variable name should start with lower case letter and start secord word with capital letter. For example, String employeeName=”Ajit”; (It should follow camel case standards)
  3. Variable name can start with speacial character like $ and _

Types of variable:

  1. Local variable
  2. Static variable (Class variable)
  3. Instance variable

1) Local variable:

The local variable is the variable which is declared inside the method and their scope is limited to that method only, which means that you cannot access or change the value of that variable outside that method.

package com.ajitation;

public class LocalVariable{

    String name = "Instance Variable"; // instance variable

    public void testMethod() {
        String name = "Local variable";
        System.out.println("name==" + name);
    }

    public static void main(String[] args) {
        LocalVariable lv = new LocalVariable();
        System.out.println("before calling testMethod method");
        lv.testMethod();
    /*
     * we are calling testMethod() where we are changing the value of name
     * And again we are printing value of name just to make justify that
     * local variable scope is limited to that method only
     */
        System.out.println("name==" + lv.name);
    }
}

Output:

before calling testMethod method
name==Local variable
name==Instance Variable

2) Static variable:

Static variable also know as Class variable because we can call it directly with class name. (Static variable are associated with class and will be common for all instances of class).

It occupies only one memory space.

package com.ajitation;

public class StaticVariable{

public static String name="static_name";

public static void main(String[] args) {
    StaticVariable obj1=new StaticVariable();
    StaticVariable obj2=new StaticVariable();
    StaticVariable obj3=new StaticVariable();

    //all 3 instances will print same name as "Static_name"
    System.out.println(obj1.name);
    System.out.println(obj2.name);
    System.out.println(obj3.name);

    //changing the value of variable using obj3
    obj3.name="static_name_changed";

    /* even if you have changed the value using obj3 only but still
     * value of name for all 3 instances will be print as "Static_name_changed"
     * because static variable occupies same memory space */
    System.out.println(obj1.name);
    System.out.println(obj2.name);
    System.out.println(obj3.name);
  }
}

Output:

static_name
static_name
static_name
static_name_changed
static_name_changed
static_name_changed

3) Instance variable:

Every object has it’s separate copy of instance variable. In below example we are changing the value of variable using obj3 only and when we print value of variable using all3 objects, it is changing the value for obj3 only, others remain unchanged.

package com.ajitation;

public class InstanceVariable{

String name="static_name";

public static void main(String[] args) {
    InstanceVariable obj1=new InstanceVariable();
    InstanceVariable obj2=new InstanceVariable();
    InstanceVariable obj3=new InstanceVariable();

    //all 3 instances will print same name as "Static_name"
    System.out.println(obj1.name);
    System.out.println(obj2.name);
    System.out.println(obj3.name);

    //changing the value of variable using obj3
    obj3.name="static_name_changed";

    System.out.println(obj1.name);
    System.out.println(obj2.name);
    System.out.println(obj3.name);
  }
}

Output:

static_name
static_name
static_name
static_name
static_name
Static_name_changed

Thanks for visiting Ajitation.com, Ajitation is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.