Variables in Java

Understand the concept of variables and naming conventions used in Java language in this lesson.

In coding example seven, we saw two fields in the class AnotherCar, speed and gear. We also learned that they are states of the car object.

Variables or fields

The car object stores its states in fields. In Java, the terms field and variable are both used. In fact, the term variable is often used more. Since we store states of an object in variables, we need to be careful about naming them. It should be meaningful. It is also a good practice. If we want to store the state of the speed of any object in a variable, the name of the variable should be ‘speed’. Writing ‘s’ instead of ‘speed’, does not convey anything.

Now, we have written the speed like this:

int speed = 0;

What does that mean? It is commonly understood that the speed of anything should be an integer value. It should not be a text string. Therefore, we have written int before the variable name. Thereafter, we have initialized the variable with a value 0. Is that necessary?

We will discuss this in the coming sections where we will talk about primitive data types like byte, short, int, etc.

Coding example: 8

Let’s talk more about the naming conventions of variables. We are going to look at two problems before you see examples eight and nine. In these examples, you will learn how to name variables in different ways.

/*
rules and conventions for naming fields in a class;
the terms variables and fields are both used
*/
class Robot {
int speed = 0;
void walk(int distance, int hour) {
speed = distance/hour;
}
void run (int distance, int hour) {
speed = distance/hour;
}
void printState() {
if(speed <= 5) {
System.out.println("The speed of robot is " + speed + ". It walks.");
}
else {
System.out.println("The speed of robot is " + speed + " kilometers per hour. It runs.");
}
}
}
public class ExampleEight {
public static void main(String args[]) {
Robot robot1 = new Robot();
robot1.walk(40, 2);
robot1.printState();
}
}

Code explanation

We have used very simple if/else logic to check whether the robot walks or runs. Because the speed of the robot is greater than five mph here.

In the above code, we have followed the normal naming conventions. However, before the variable names, we can use $ or _ signs.

Coding example: 9

/*
rules and conventions for naming fields in a class;
the terms variables and fields are both used
*/
class RobotOne {
int speed = 0;
void walk(int $distance, int _hour) {
speed = $distance/_hour;
}
void run(int $distance, int _hour) {
speed = $distance/_hour;
}
void printState() {
if(speed <= 5) {
System.out.println("The speed of robot is " + speed + ". It walks.");
}
else {
System.out.println("The speed of robot is " + speed + " kilometers per hour. It runs.");
}
}
}
public class ExampleNine {
public static void main(String args[]) {
RobotOne robot1 = new RobotOne();
robot1.walk(10, 2);
robot1.printState();
}
}

Code explanation

The above code breaks the normal conventions of naming the variables and uses special characters like $ and _.

Meaningful variable names

The convention is you should always begin your variable name with a letter. Using $ or _ is not encouraged. As we said before, the name of a variable should be meaningful. Using cryptic abbreviations doesn’t make any sense and makes it harder for everyone. Try to be clear and expressive while you are writing your code.

The naming convention consists of two major things: we write a class like this - NewClass, especially when the class name has more than two words. The same is true for a variable, only one word should begin with a lowercase letter. If there are two words, write it like this: ‘twoWords’.

Adding comments in Java

For comments, you can use both:

/*
Inside goes your long comments
/*
// use it for short comments

Adding comments in proper places is also a good habit.