Java – Variable Types

Variables in Java

I’m sure you already know what a variable in Java is. Just a memory location to store values. However, you have the power to provide a name for variables. Additionally, you can also specify the data type of variables.

We are going to examine:

  1. Local Variables
  2. Instance Variables
  3. Class Variable (also called Static Variables)

 

1. Local Variables

Note the following properties of local variables:

  • Declared inside block of codes. For example inside, methods or constructors
  • Not visible outside the block
  • Cannot be used with access modifiers
  • Implemented internally using a stack
  • Best to initialize it when declared

Let’s take an example

public class Tester {

	public void IncreasePay(int pay) {
		int amount = 100; // local variable
		int newPay = pay + amount; //local variable
		System.out.printf("Pay has increased from %d to %d ", pay, newPay);
	}
	
	public static void main(String[] args) {
		amount = 200;  //This line gives error
		newPay = 600;  //This line gives error
		Tester t = new Tester();
		t.IncreasePay(1000);
	}
}

Listing 1.0: Local variables example

In Listing 1.0, variables amount and newPay are both local variables. They are only available inside the IncreasePay block. Therefore, if you try to use them inside the main method, you get an error.  

 

2. Instance Variables

We declare instance variables inside a class. However, they must be outside a method.

I provide the following properties of instance variables

  • Declared within a class
  • Declared outside a method
  • Also declared outside a constructor
  • Memory is allocated for instance variables when an object is created. This is normally on the heap
  • You create them with the ‘new’ keyword
  • Destroyed when the object goes out of scope
  • You can define them using access modifiers
  • Can be declared in class level before or after use
  • Visible to all blocks in the class. Such as constructors and methods
  • It’s best to declare them as private or protected
  • They have default values. This depends of he data type.
  • Can be accessed from within the class

Example of instance variable is given in Listing 1.1.

public class Triangle {
	private int height;
	private int base;
	
	public Triangle() { //constructor
	}
	
	public double Area() {
		double Area = (1/2)*base*height;
		return Area;
	}
}

Listing 1.1: Example of Instance variable

We see two instance variables in Listing 1.1. They are height, and base. You can see they are used inside the Area method. Also notice they are declared as private.  

 

3. Class Variables

Also known as static variables. I provide a list of attribute for class variables below.

  • You declare them with static keyword
  • Declare outside code block such as method and constructors
  • There is only on copy per class.
  • Static variables are stored in static memory.
  • You used them in a similar way to constants.
  • They are created when the program starts running.
  • They are destroyed when program execution ends.
  • You access them using the class name. For example Vehicle.make. That is <class_name>.<variable_name)
  • Generally declared as public.
  • Has default values depending on the data type.
  • They are seldom used

I provide an example is given below. You can try to run it yourself.

public class Vehicle {
	String make;
	public static int numberOfVehicles = 0; // Static Variable
	
	public Vehicle(String make) {
		this.make = make;
	}
}

Listing 1.2: Example of Static Variable