Introduction to Java: Variables

When performing simple math calculations, we usually store certain results in variables for later use. The same logic applies to programming languages.

In Java, we store users’ inputs, results, and values into variables. When we store a value in a variable, we can easily retrieve this value.

In this tutorial, we will learn about variables in Java.

1. Variables in Java

A variable represents a piece of memory that stores some value.

In Java, we distinguish three types of variables, depending on the place they occur:

  • Instance variables
  • Local variables
  • Static variables

Every variable has a name and type:

int count;

Here, we declared a variable.

To initialize a variable, we need to assign a value to the variable:

int count = 10;

1.1 Naming Convention

There are rules for almost anything, including naming our variables. Naming rules are the same as the ones when naming methods:

  1. The first character can be a letter or symbol $ or _.
  2. We can use numbers, however, they can not be at the beginning.
  3. We can not use reserved words.

2. Instance Variables

An instance variable represents a variable defined within a class.

What is more, when defining a variable, it does not need to be initialized explicitly. If we do not provide a value, Java will use a default value.

Furthermore, the default value varies depending on the data type:

Type

Default value

boolean

false

byte, short, int, long

0

float, double

0.0

char

0

reference

null

For example, let us consider the Food class:

public class Food {

    String name; // default value is null

    String type = "Fruit"; // does not have default value since we assign a value

    double kilocalories; // default value is 0.0

    int preparationTimeSeconds; // default value is 0

}

Here, we have four instance variables. The default value of variables with the String type is null. Additionally, for the int data type, the default value is 0, and for double is 0.0.

Moreover, the instance variables are available from the moment we declare them until the life cycle of the object ends.

3. Local Variables

Going further, a local variable is a variable within a method.

Now, unlike instance variables, local variables don’t have the default values. Therefore, we should always initialize them before using them. Before we assign a value to a local variable, it contains data from the memory. Java strictly forbids to access these values.

Additionally, if we don’t assign a value to a variable before we use it in our code, our code will result in compiler errors.

The scope of a local variable is within a method where it is defined. In other words, we cannot access them outside a method:

public class Dog {

    public int calculateAge(int humanAge){
        int multiplier = 7;
        int realAge = humanAge * multiplier;
       return realAge;
    }

}

Furthermore, the scope can be even smaller – for instance, the variable defined inside an if statement can be used only inside this if statement:

public int calculateAge(int humanAge) {
    if (humanAge == 0) {
        int age = 0;
        return age;
    }
    int multiplier = 7;
    int realAge = humanAge * multiplier;
    return realAge;
}

Parameters of the method are also considered to be local variables – we can’t access them outside the method they’re defined.

4. Static Variables

Lastly, let’s talk about static variables, also known as constants. Just like instance variables, static variables are defined within a class. In other words, we need to define them on a class level. Therefore, they’re not suitable for methods.

The main difference between instance and static variables is the static keyword. With the static keyword, we define a variable/method we can use without instantiating a class. To put it differently, we don’t require a new object to access them.

Static members of a class belong to the class itself and not the objects created from this class.

When we talk about scope, they are available from the moment they’re declared until the end of the program. Additionally, if we don’t initialize them, they will have a default value.

Now, what’s the point of having static variables? Well, you can simply use them for the values that won’t change. For instance, if we have a Zoo with a maximum animal capacity of 100, we can define this number as a static variable.

public class Zoo {
    public static int MAX_CAPACITY = 100;
}

5. Key Takeaways

In this tutorial, we learned about different variable types in Java.

To sum up, instance variables are the ones we define in the class. They have default values. Similarly, we have static variables that use the static keyword and we don’t need to create an object in order to use them. Lastly, local variables are the ones defined inside a method and thus, we can’t use them outside the method they’re defined.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x