Variables in Java
Table of Contents
Variables in Java
Variable in Java
In this Tutorial we will learn about Java Variables, various types of Variables along with code examples to understand Java Variables.
What is a Variable?
When we want to save any information, we save it in a Computer’s address. We give that address a name so we don’t have to remember the complicated address where we have our information stored. Variable refers to how an address is named. The name of the memory location is Variable.
In other words, Variable is a name which is used to store a value of any type during program Execution.
To declare the Variable in Java, we can use following syntax
datatype variableName;
Here, datatype refers to type of variable which can any like: int, float etc. and variableName can be any like: empId, amount, price etc.
Java Programming language defines mainly three kind of Variables.
- Instance Variables
- Static Variables (Class Variables)
- Local Variables
Instance Variables in Java
Variables that are Declared inside a class but outside of any methods, Constructors, or blocks are known as instance Variables. A Variable of an object known as a field or property is also known as an instance Variable. Object Variables are the name given to them. Since each object has a copy of each Variable, changing the value of a Variable in one object has no impact on the instance Variable.
class Student
{
String name;
int age;
}
Here name and age are instance variable of Student class.
Static Variables in Java
Class variables that are declared as static are called static. Static variables only need to be initialised once. Along with the final keyword, static variables are also used to declare constants.
class Student
{
String name;
int age;
static int instituteCode=1101;
}
Here instituteCode is a static variable. Each object of Student class will share instituteCode property.
Additional points on static variable:
- static variable are also known as class variable.
- static means to remain constant.
- In Java, it means that it will be constant for all the instances created for that class.
- static variable need not be called from object.
- It is called by classname.static_variable_name
Note: A static variable can never be defined inside a method i.e it can never be a local variable.
Example:
Consider creating two Student class objects and changing the value of a static variable in one of them. Printing it from another object will now print the updated value. This is due to the fact that it was declared static, meaning it applies to every new object.
package onlineexamguide;
class Student{
int a;
static int id = 35;
void change(){
System.out.println(id);
}
}
public class onlineexamguide {
public static void main(String[] args) {
Student o1 = new Student();
Student o2 = new Student();
o1.change();
Student.id = 1;
o2.change();
}
}
35 1
Local Variables in Java
In a block, constructor, or method, local variables are declared. Local variables are created at the beginning of a method, constructor, or block and destroyed at the end. Stack houses local variables. Local variables don’t use access modifiers.
float getDiscount(int price)
{
float discount;
discount=price*(20/100);
return discount;
}
Here discount is a local variable.
Variable Scope in Java
Scope of a variable decides its accessibility throughout the program. As we have seen variables are different types so they have their own scope.
Local variable: Scope of local variable is limited to the block in which it is declared. For example, a variables declared inside a function will be accessible only within this function.
Instance variable: scope of instance variable depends on the access-modifiers (public, private, default). If variable is declared as private then it is accessible within class only.
And If variable is declared as public then it is accessible for all and throughout the application.
If variable is declared as default the it is accessible with in the same package.
For more details about accessibility you can refer our detailed tutorial.
Example:
So In this example, we created two variables a and i, first is declared inside the function and second is declared inside for loop. Both variables have their own scope in which they are declared, so accessing outside the block reports an error.
public class HelloWorld {
public static void main(String[] args) {
int a = 10;
for(int i = 0; i<5; i++) {
System.out.println(i);
}
System.out.println("a = "+a);
System.out.println("i = "+i); // error
}
}
error: cannot find symbol i