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
- 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
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
If we add another modifier final, the variabke voltage is now considered constant which means to say this variable cannot be changed.
Local Variables
Parameters
Rules 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.