8) Variables

What is a variable?

When we want to store any information, we store it in an address of the computer. Instead of remembering the complex address where we have stored our information, we name that address. The naming of an address is known as variable. Variable is the name of memory location.

Variable Declaration

To define a variable, we need to assign a data type for that variable. Data type defines the kind of value this variable can hold (int, long or String etc.).

Example of variable Declaration

public final int var ;

public : Access Modifier applied to variable

final : Non Access Modifier applied to this variable

int : Data type. It defines kind of value this variable can hold (int in this case)

var : Name of the variable

Variable Initialization

Now we are done with defining a variable, we can initialize the above variable by assigning a value to it. In this case, we assign the variable an integer value.

public final int var = 9;

Rules to declare a Variable

  • Every variable name should start with either alphabets or underscore (_) or dollar ($) symbol.

  • No space is allowed in the variable declarations.

  • Except underscore ( _ ) no special symbol are allowed in the middle of variable declaration

  • Variable name always should exist in the left hand side of assignment operators.

  • Maximum length of variable is 64 characters.

  • No keywords should access variable name.

Note: Actually a variable also can start with ¥,¢, or any other currency sign.

Types of Variables

Variables in Java can be defined anywhere in the code (inside a class, inside a method or as a method argument) and can have different modifiers. Depending on these conditions variables in Java can be divided into four categories.

  1. Instance Variable

  2. Static Variable

  3. Local Variable

  4. Method Parameter

Instance Variable (Non Static Fields)

Instance variables are used by objects to store their states. Variables which are defined without the STATIC keyword and are outside any method declaration are object specific and are known as Instance Variables. Such variables are called instance variables because their values are instance specific and values of these variables are not shared among instances.

Example of Instance variable

package com.gs.ilp.corejava.variables;

public class InstanceVarExample {
	String myInstanceVar = "instance variable";

	public static void main(String args[]) {
		InstanceVarExample obj = new InstanceVarExample();
		InstanceVarExample obj2 = new InstanceVarExample();
		InstanceVarExample obj3 = new InstanceVarExample();
		System.out.println(obj.myInstanceVar);
		System.out.println(obj2.myInstanceVar);
		System.out.println(obj3.myInstanceVar);
		obj2.myInstanceVar = "Changed Text";
		System.out.println(obj.myInstanceVar);
		System.out.println(obj2.myInstanceVar);
		System.out.println(obj3.myInstanceVar);
	}
}

Output:

Class Variable (Static Fields)

Variables which are declared with a static keyword inside a Class (outside any Method) are known as Class variable / Static variable. They are known as Class level variables because values of these variables are not specific to any instance but are common to all instances of a class. Such variables will be shared by all instances of an Object.

package com.gs.ilp.corejava.variables;

public class StaticVarExample {
	public static String myClassVar = "class or static variable";

	public static void main(String args[]) {
		StaticVarExample obj = new StaticVarExample();
		StaticVarExample obj2 = new StaticVarExample();
		StaticVarExample obj3 = new StaticVarExample();
		// All three will display "class or static variable"
		System.out.println(obj.myClassVar);
		System.out.println(obj2.myClassVar);
		System.out.println(obj3.myClassVar);
		// changing the value of static variable using obj2
		obj2.myClassVar = "Changed Text";
		// All three will display "Changed Text"
		System.out.println(obj.myClassVar);
		System.out.println(obj2.myClassVar);
		System.out.println(obj3.myClassVar);
	}
}

Output:

As you can see all three statements displayed the same output irrespective of the instance through which it is being accessed. That is why we can access the static variables without using the objects like this:

System.out.println(myClassVar);

Note: A static variable can never be defined inside a method i.e it can never be a local variable.

Local Variables (Method Local)

When a variable is declared inside a Method it is known as Method Local Variable. The scope of local variables is only inside the Method, which means local variables cannot be accessed outside that Method. There are some restrictions on access modifier that can be applied on local variables.

package com.gs.ilp.corejava.variables;

public class VariableExample {
	// instance variable
	public String myVar = "instance variable";

	public void myMethod() {
		// local variable
		String myVar = "Inside Method";
		System.out.println(myVar);
	}

	public static void main(String args[]) {
		// Creating object
		VariableExample obj = new VariableExample();
		/*
		 * We are calling the method, that changes the value of myVar. We are displaying
		 * myVar again after the method call, to demonstrate that the local variable
		 * scope is limited to the method itself.
		 */
		System.out.println("Calling Method");
		obj.myMethod();
		System.out.println(obj.myVar);
	}
}

Output:

If I hadn’t declared the instance variable and only declared the local variable inside method then the statement System.out.println(obj.myVar); would have thrown compilation error. As you cannot change and access local variables outside the method.

Parameters

Parameters are variables that are passed in Methods. For example, String args[] variables in the main Method is a parameter.

Last updated