C
C
Core Java
Search…
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
1
package com.gs.ilp.corejava.variables;
2
3
public class InstanceVarExample {
4
String myInstanceVar = "instance variable";
5
6
public static void main(String args[]) {
7
InstanceVarExample obj = new InstanceVarExample();
8
InstanceVarExample obj2 = new InstanceVarExample();
9
InstanceVarExample obj3 = new InstanceVarExample();
10
System.out.println(obj.myInstanceVar);
11
System.out.println(obj2.myInstanceVar);
12
System.out.println(obj3.myInstanceVar);
13
obj2.myInstanceVar = "Changed Text";
14
System.out.println(obj.myInstanceVar);
15
System.out.println(obj2.myInstanceVar);
16
System.out.println(obj3.myInstanceVar);
17
}
18
}
Copied!
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.
1
package com.gs.ilp.corejava.variables;
2
3
public class StaticVarExample {
4
public static String myClassVar = "class or static variable";
5
6
public static void main(String args[]) {
7
StaticVarExample obj = new StaticVarExample();
8
StaticVarExample obj2 = new StaticVarExample();
9
StaticVarExample obj3 = new StaticVarExample();
10
// All three will display "class or static variable"
11
System.out.println(obj.myClassVar);
12
System.out.println(obj2.myClassVar);
13
System.out.println(obj3.myClassVar);
14
// changing the value of static variable using obj2
15
obj2.myClassVar = "Changed Text";
16
// All three will display "Changed Text"
17
System.out.println(obj.myClassVar);
18
System.out.println(obj2.myClassVar);
19
System.out.println(obj3.myClassVar);
20
}
21
}
Copied!
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:
1
System.out.println(myClassVar);
Copied!
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.
1
package com.gs.ilp.corejava.variables;
2
3
public class VariableExample {
4
// instance variable
5
public String myVar = "instance variable";
6
7
public void myMethod() {
8
// local variable
9
String myVar = "Inside Method";
10
System.out.println(myVar);
11
}
12
13
public static void main(String args[]) {
14
// Creating object
15
VariableExample obj = new VariableExample();
16
/*
17
* We are calling the method, that changes the value of myVar. We are displaying
18
* myVar again after the method call, to demonstrate that the local variable
19
* scope is limited to the method itself.
20
*/
21
System.out.println("Calling Method");
22
obj.myMethod();
23
System.out.println(obj.myVar);
24
}
25
}
Copied!
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 modified 2yr ago
Copy link