Java Variable Scope

62
views
Java Variable Scope

In this tutorial, you’ll learn about Java Variable Scope and its types i.e Local Variable, Instance Variable, class/static Variable.

The scope of a variable or (Variable Scope) defines the section of the code in which the variable is visible. As a general rule, variables that are defined within a block are not accessible outside that block.

There are three kinds of variables in Java:

  • Local Variable
  • Instance Variable
  • class/Static Variable

Local Variable:

  1. A variable that is declared inside the method is called local variable.
  2. 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.
  3. Access modifiers cannot be used for local variables.
  4. Local variables are visible only within the declared method, constructor or block.
  5. There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.

Here, the age is a local variable.

This is defined inside pupAge( ) method and its scope is limited to only this method.

Program to Find the Age

public class Test {
   public void pupAge() {
      int age = 0;
      age = age + 7;
      System.out.println("Puppy age is : " + age);
   }

   public static void main(String args[]) {
      Test test = new Test();
      test.pupAge();
   }
}

Output

Puppy age is: 7

Instance Variable:

  1. A variable that is declared inside the class but outside the method is called instance variable. It is not declared as static.
  2. Instance variables are created when an object is created with the use of the keyword ‘new’ and destroyed when the object is destroyed.
  3. Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object’s state that must be present throughout the class.
  4. Instance variables can be declared at the class level before or after use.
  5. Access modifiers can be given for instance variables.
  6. Instance variables have default values. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor.
  7. Instance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class ( when instance variables are given accessibility) should be called using the fully qualified name.
  8. Instance variables can be accessed by calling with the class name ObjectReference.VariableName.

Program to Display Employee Name and Salary

import java.io.*;
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 empOne = new Employee("Ransika");
      empOne.setSalary(1000);
      empOne.printEmp();
   }
}

Output

name  : Ransika
salary :1000.0

Class/Static Variable:

  1. A variable that is declared as static is called static variable. It cannot be local.
  2. Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
  3. There would only be one copy of each class variable per class, regardless of how many objects are created from it.
  4. Static variables are stored in static memory.
  5. Static variables are created when the program starts and destroyed when the program stops.
  6. Visibility is similar to instance variables.
  7. Static variables can be accessed by calling with the class name ClassName.VariableName.

Program to find average Salary using class/static Variable

import java.io.*;
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);
   }
}

Output

Development average salary:1000

Program using All 3 Variables

class A{
int data=50;
  //instance variable
static int m=100;
  //static variable

void method(){
int n=90;
  //local variable
}
}  //end of class A

Ask your questions and clarify your/others doubts on for loop in Java Variable Scope by commenting. Documentation.

PREVIOUS
JAVA VARIABLE
NEXT
COMING SOON…