Variable types in Java

Variable is a name of memory location where the value is stored. Each variable in Java has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable. In this chapter, we will see various types of variable types in Java. There are three different kinds of variable in Java:
  1. Local variables
  2. Instance variables
  3. Class/static variables
Variables in java

1) Local variables
  • Local variables are declared in methods, constructors, or blocks.
  • Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
  • Access modifiers cannot be used for local variables.
  • The scope of local variable is only within the declared method, constructor or block.
Example:
In following example, age is a local variable which is defined inside Age() method and having its scope limited to the Age() method only.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class Dog {
 public void Age() {
  int age = 0;
  age = age + 7;
  System.out.println("Dog's age is : " + age);
 }

 public static void main(String args[]) {
  Dog dog = new Dog();
  dog.Age();
 }
}
This would produce the following result:
Dog's age is: 7

2) Instance variables
  • Instance variables are declared in a class, but outside a method, constructor or any block.
  • When a space is allocated for an object in the heap, a memory slot for each instance variable value is created.
  • Instance variables are created when an object is created and destroyed when the object is destroyed.
  • Instance variables hold values that must be referenced by more than one method, constructor or block.
  • Access modifiers can be given for instance variables.
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Employee{
 
 // this instance variable is visible for any child class.
 public String name;
 
 // salary variable is visible in Employee class only.
 private double salary;
 
 // The name variable is assigned in the constructor.
 public Employee(String empName) {
  name = empName;
 }
 
 // The salary variable is assigned a value.
 public void setSalary(double empSal) {
  salary = empSal;
 }
 
 // This method prints the employee details.
 public void printEmp() {
  System.out.println("name : " + name);
  System.out.println("salary :" + salary);
 }

 public static void main(String args[]) {
  Employee emp1 = new Employee("Ransika");
  emp1.setSalary(1000);
  emp1.printEmp();
 }
}

This would produce the following result:
name : Ransika
salary :1000.0

3) Class/static variables
  • Class variables is also known as static variable and declared with the static keyword in a class, but it is outside method, constructor or a block.
  • Only one copy of each class variable is created, regardless of how many objects are created.
  • Static variables are stored in static memory. 
  • Static variables are created when the program starts and destroyed when the program stops.
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class Employee {

 // salary variable is a private static variable
 private static double salary;

 // DEPARTMENT is a constant
 public static final String DEPARTMENT = "Development ";

 public static void main(String args[]) {
  salary = 1000;
  System.out.println(DEPARTMENT + "average salary:" + salary);
 }
}

This would produce the following result:
Development average salary:1000


<-- Previous || Next -->

4 comments:

  1. Instance variables hold values that must be referenced by more than one method, constructor or block.
    What does this mean?

    ReplyDelete
    Replies
    1. Hi Hemant,

      In simple term, The instance variables can be accessible in instance method and constructor. I hope this clears your doubt.

      Thanks,
      Vinod

      Delete
  2. In Local variables section, everywhere it is mentioned: "constructor OR block". So it is like that, that in a class local variable can be declared only in one of them - constructor or block? Or is it like that constructor and block can not be used together in a class?
    I think they both can be used in the same class?

    ReplyDelete
  3. Hi Vivek,

    The variable which is defined in methods, constructor or block is known as local variable. The scope of the variable is limited to that method or constructor or block.

    If you wnat to use that variable globally in a class then you will use instance or static variable which can be accessed by any method or block.

    I hope this clears your query. Please keep on writing, if you have any suggestions or queries on Selenium and Java.

    Thanks,
    Vinod Rane.

    ReplyDelete