From previous section we tackled the DeskFan class. Remember that we have the following variable declaration

boolean powerOn = false;
int currentGearSpeed = 3;
boolean isOscillating = true;

These are the fields where objects stored it’s state. For the overall course of this java tutorial we will be using fields and variables alternately. They refer to the same thing.

Topics Covered

At the end of this tutorial we should be able to fully understand the following:

  • other than boolean and int, what are the other variable type we could use
  • what are the rules governing variable declaration
  • can we reuse variable?
  • Scope of variable

Let’s take a look on our previous example the DeskFan class with additional fields.

package com.javatutorial.example;

/*
 * DeskFan object definition
 */

public class DeskFan {
	boolean powerOn = false;
	int currentGearSpeed = 3;
	boolean isOscillating = true;
	static int voltage = 220;

	void turnOn(boolean newVal) {
		powerOn = newVal;
	}

	void changeGearSpeed(int newVal) {
		currentGearSpeed = newVal;
	}

	void changeOscillation(boolean newVal) {
		isOscillating = newVal;
	}

	public void showState() {
		System.out.println("Printing the current state of object DeskFan");
		System.out.println("Is it powerOn:" + powerOn);
		System.out.println("What is the current speed:" + currentGearSpeed);
		System.out.println("Is it oscillating:" + isOscillating);
	}
}

 

From the above example let’s classify all the fields that has been used.

Instance variable

Instance variables are those variables that is declared non statically. These static fields is where java objects stores it’s state. These variables are unique in nature such that for every instance of an object a copy of these variables are created.

We would be able to recognize instance variable by just looking on those variables outside any method which usually comes just after the opening bracket of any class and without the keyword static. From the DeskFan class,  the variables powerOn, currentGearSpeed and isOscillating are all instance variables which you can find in line 9,10 and 11.

Class Variables

A class variable can be found on the same location as that of the instance variable. The only difference is that a class variable is declared using keyword static. Using the static modifier will tell the compiler that this variable has only one copy in existence. So regardless of how many copy of a class is created, there would be only one value of this variable. Since the class variable is static, it  should be accessed statically. Taking a look on the DeskFan example, we have declare a class variable as static int voltage = 220; . In order for us to access the voltage variable we should used the ClassName for example DeskFan.voltage

If we add another modifier final, the variabke voltage is now considered constant which means to say this variable cannot be changed.

Local Variables

This variable is being used by method to stores it’s states. This method is very similar to class variables but the only difference is the placement. While the class variable can be accessed and modified on all the places within the class, the local variables is only accessible at method level. So for instance if we have declared an int value = 5; the variable value is only visible to the method where it is declared.

Parameters

This is considered as local variable, we cannot reuse this variable on the same method wherein it was declared. But what is the purpose of this variable? It would be used to accept values from the calling method. We have used a lot of this variable from our DeskFan class. One example is void changeGearSpeed(int newVal); The newVal is what we call Parameters. From the Hello World application alone, we have used a parameter which is String[] args from main method.

Rules in Naming a Variable

The following are the set of rules that need to be strictly followed in naming a variable:

  • Variable Names are case sensitive.
  • It can be any legal identifier
  • It can be of any length
  • First character can be any letter or number. We can also used _ or $.
  • White Space is not permitted
  • Variable cannot used keyword or we call them reserved words.

Java Naming Convention

Although the following are allowed in naming a variable, there are set’s of standards or conventions foverning the use of variables. This has been placed by java community for the sole purpose of code readability. Even though you can still not follow below convention, it is discouraged.

  • start with lower case letter
  • do not use $ in variable name though this is allowed but never used at all by java community.
  • limit the use of _, the use of this character is highly discouraged not unless the variable is constant.
  • use of all caps in declaring a constant. If it is two word variable, separate it using _.
  • it is highly encouraged to use full words in naming a variable.
  • if the variable is two words, start with a lower case character for the first word and uppercase letter for the first character of second word. This is camel case. For example secondVariable.