Java Variables

The Java Containers for Storing Data

In computing, variables are just like containers which store data in the memory addresses. Variables can hold different kinds of data, for instance, integer numbers, double-precisions, characters, String, etc.

When you declare (create) a variable, simply the declared variable reserve storage from the computer memory (RAM) for storing data.  So using variables you can play with data in programming.

Basic Characteristics of Variables

Almost all the high-level programming languages make use of the variable feature. Variables have some basic characteristics in programming including Java:

  • A variable name is just a name given by programmers. It just indicates the reserved allocated memory addresses. All the data are stored in the indicated memory addresses not in the name actually.
  • A variable data can be changed by re-declaring (re-assigning values) the variable or by overriding. And that’s why it is called variables. Because it came from vary + able.
  • For declaring (creating) variables, programmers must follow the variable naming syntax, Which varies language to language. We will learn java variable naming syntax in a few moments.
  • In some languages, variables are must be declared before used like Java. But in some language variables can be declared while use.

Java Variables

To use variables in java, variables must be declared first. Here how we can declare variables in Java.

declare java variables

From the image, we saw the variable declaration syntax. Let’s discuss the parameters

Data Types: The type of data you want to store in a variable. For example, if you want to store some number in a variable, then you have to use int as the integer data type. In the above example, we have used String as data type which stores some string (text) data.

INFO: We will learn more about data types in the Java data types chapter.

Variable name: is the given name of a variable. In java, you have to follow the below rules while declaring variables.

  • Variables must begin with laters (A-Z, a-z) or a dollar sign ( $ ) or underscore ( _ ).
  • After the first character from the above boundary, any combination of characters can be used including numbers.
  • Numbers can not be seat at the first of a variable name.
  • Example of some valid variables are: count, myName, $amount, _age, TotalAmout.
  • Example of some invalid variables are: 1count, -name.

Initialization of Java Variables

So far we have learned how to declare and name variables in Java. Now it is time to initialize the variable by assigning values in it. We can assign values in a variable using two ways. See the example below:

int myNumber;
myNumber = 15;

double myFloat = 56.55;

System.out.println(myNumber);
System.out.println(myFloat);

Notice that we can declare a variable first and then we can initialize the variable by assigning values later. We can also initiate the variable while declaring the variable like the above.

Overwriting Java Variables

We knew that the value of a variable can be changed by overwriting. Here How it works in Java;

int myNumber;
myNumber = 15; // The value of myNumber is now 15
System.out.println(myNumber); // Output: 15
        
// re-assigning the value
myNumber = 44; // The value of myNumber is now 44
System.out.println(myNumber); // Output: 44

Types of Java Variables

In Java, you can create three types of variables. They are different in behavior and function.

  • Local variables – declared inside a method and can be accessed in the method only.
  • Instance variables – declared in a class instead of any method, block, or constructor. It is a non-static variable.
  • Static variables – same as instance variables but it is static.

Local Variables

When a variable declared inside a method, block or constructor then it is called local variable.

  • Java local variables can be accessed from inside of the method, block, or constructor only where the variable is declared.
  • All the local variables inside a method are created if only the method is called from the main method.
  • Access modifiers like a public, private can not be used in local variables.
  • Local variables can not be static.
  • When a method, block, or constructor ends execution, all the local variables in that method, block or constructor are destroyed.
  • If you work with an uninitialized local variable, you will get an error because local variables have no default value. So always assign an initial value to a local variable.
Example (localVar.java)
public class localVar {
    // getNum method
    public void getNum() {
        int num = 10; // local variable in getNum() method
        System.out.println(num);
    }
    public static void main(String args[]) {
        localVar obj = new localVar();
        obj.getNum(); // entering methods using objects

        int num1 = 26; //another local variable but in main method
        System.out.println(num1);

    }
}
Output
10
26

Now if we want to access a local variable from the outside of a method you will get an error. See the example below;

Example (localVar.java)

public class localVar {
    // getNum method
    public void getNum() {
        int num = 10; // local variable
        System.out.println(num);
    }
    public static void main(String args[]) {
        // accessing num variable from main method
        // the num variable is declared in getNum() method
        // but we are trying to access its value from
        // outside of the method.
        System.out.println(num);
    }
}
Output

localVar.java:12: error: cannot find symbol
System.out.println(num);
^
symbol: variable num
location: class localVar
1 error

Instance Variables

When a variable is declared in a class but not in any methods, blocks or constructor then it is called instance variables.

  • Instance variables only can be accessed using objects outside of the class.
  • Instance variables can have access specifiers like private, public, etc.
  • Instance variables are object-oriented features in Java.
  • Instance variables can be accessed directly in the current class and sub-classes even in methods, block or constructor.
  • The default value of an instance variable is (for numbers 0, for objects null, for boolean false). So the initial value assignment is not mandatory.
  • Instance variables can prevent to be accessed from the outside of the class using the private specifier.

Example (instanceVar.java)

class employee {
    public String name;
    public int age;
    public double salary;

    // assigning name in the constractor
    employee() {
        name = "Mehrab";
    }

    // assigning age and salary in a method
    void setInfo() {
        age = 24;
        salary = 50000;
    }
}

public class  instanceVar {
    public static void main(String args[]) {
        // creating object
        employee info = new employee();

        // getting values from instance variables
        System.out.println(info.name);
        info.setInfo();
        System.out.println(info.age);
        System.out.println(info.salary);
    }
}
Output
Mehrab
24
50000.0

What is Next?

So far, we have learned how all the variable types work in Java programs. To learn variables, we have discussed the data type and rarely we have worked with integers, double, and String data types. Java can hold some more types of data. In the next chapter, we will learn about Java data types in detail.

Posted in