Rules For Variable Declaration in Java


In any programming language, variables serve as a container that is used to store values. Before actually using a variable we need to declare it first. We can’t give any random names or symbols to it, although Java provides almost all the letters and symbols available for human and machine use.

We will explore the naming conventions and set of rules for variable declaration in Java through this article.

Variable Declaration in Java

When it comes to declaring a variable, we need to define its type first, then name of the variable and an optional value. We can declare a variable first and initialize it with data wherever we need it in the whole program. Also, we can declare and initialize at the same time. If we need to declare multiple variables of the same type then, we simply chain them using commas as a separator in a single line.

Syntax

dataType nameOfvariable;       

It is only the declaration. Here, ‘datatype’ signifies type of data i.e. integer, double, char and so forth.

Syntax

dataType nameOfvariable = value;      

It is the declaration and initialization both. We can initialize using assignment operator, however, other options are also available.

Example 1

public class Var {
   public static void main(String []args) {
      int n1, n2, mult; // only the declaration 
      // initialization in another line
      n1 = 6;  
      n2 = 15;
      mult = n1 * n2;
      System.out.println("Value of n1 and n2 multiplication is: " + mult);
   }
}

Output

Value of n1 and n2 multiplication is: 90

In the above example, we have declared the variables first and in the next line we have initialized them.

Example 2

public class Var {
   public static void main(String []args) {
      //  declaration and initialization both 
      int n1 = 8;
      int n2 = 15;
      int mult = n1 * n2;
      System.out.println("Value of n1 and n2 multiplication is: " + mult);
   }
}

Output

Value of n1 and n2 multiplication is: 120

In the above code, we have declared and initialized all the variables individually.

Example 3

The following example demonstrates, how we can declare a variable as a parameter of method.

public class Var {
   public static void methodA(int n1Data, int n2Data) {
      int mult = n1Data * n2Data;
      System.out.println("Value of n1 and n2 multiplication is: " + mult);
   }
   public static void main(String []args) {
      int n1 = 8;
      int n2 = 15;
      methodA(n1, n2);
   }
}

Output

Value of n1 and n2 multiplication is: 120

Rules for Variable Declaration

Java creators have defined some set of rules to name a variable (also known as an identifier). Whoever works with Java, needs to follow that rules. Let’s discuss them one by one.

  • We can begin the variable name with a dollar sign ($), an underscore (_) or any alphabetical letters (a - z).

  • The next characters can be numbers from 0 to 9, dollar sign, underscore or any letters from A to Z. But, for better readability and understanding use alphabets rather than the combination of all symbols and numerics.

  • The use of Java keywords such as static, public, private or final are prohibited as they have special meanings in Java.

  • The white spaces are not allowed in between the letters.

  • Don’t use capital letter as the first character for naming a variable, the names of classes and Interfaces starts with capital letters.

  • Also, we cannot use digits as a first character.

Examples of legal identifiers − fruits, $user, _myVar, _12Name etc.

Examples of illegal identifier − 12name, User, public etc.

Conclusion

The name of a variable is associated with a memory location. Their names are case sensitive. In this article, we have understood variables and also we discovered how we can declare and initialize them properly.

Updated on: 12-May-2023

685 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements