Variables in Java with Java Variable Declaration Rules


1. Objective

In this tutorial for variables in Java, we are going to learn what is variable in java and different types of java variables that are used in the Java language, how to declare java variables and use them in java programs.

Variables in Java

Variables in Java

2. What are Variables in Java

Variables in Java are basically memory allocations, named storages which can be later manipulated by the program. Each java variable has a specific type, which determines its memory and the data that can be stored in that variable. All variables should be declared beforehand in Java.

a. Java Variable Declaration

There are 3 ways to declare a variable

  • Datatype – The Java Datatype that can be stored in that variable.
  • Variable_name – The name of the Java variable.
  • Value – The initial value stored in the variable.

Example

float simpleInterest; //Declaring float variable
int time = 10, speed = 20; //Declaring and Initializing integer variable
char var = 'h'; // Declaring and Initializing character variable

Variables in Java

Java Variable declartion

3. Types of variables in Java

There are three types of Java variables, they are

Types of variables in Java

Types of Variables in Java

a. Local variables in Java

Local variables are defined within a java method, block, or constructor and their scope remains limited to java blocks, classes in Java, and java constructors only.

These Java variables are created when a function or block is called and deleted once they exit the block or call is returned. Before discussing the example, let’s revise Java Operators and Java Access Modifiers.

Example

public class StudentDetails
{
public void StudentAge()
{   //local variable age
int age = 0;
age = age + 5;
System.out.println("Student age is : " + age);
}
public static void main(String args[])
{
StudentDetails obj = new StudentDetails();
obj.StudentAge();
}
}

b. Instance variables in Java

Java Instance variables are declared in a class outside any method, constructor, or block, it is of static type. These variables in Java are created when a object is created and destroyed once the object is destroyed. We can define the access specifiers and if nothing is mentioned than the default specifier is defined.

Example

import java.io.*;
class Marks
{
//These variables are instance variables.
//These variables are in a class and are not inside any function
int engMarks;
int mathsMarks;
int phyMarks;
}
class MarksDemo
{
public static void main(String args[])
{   //first object
Marks obj1 = new Marks();
obj1.engMarks = 50;
obj1.mathsMarks = 80;
obj1.phyMarks = 90;
//second object
Marks obj2 = new Marks();
obj2.engMarks = 80;
obj2.mathsMarks = 60;
obj2.phyMarks = 85;
//displaying marks for first object
System.out.println("Marks for first object:");
System.out.println(obj1.engMarks);
System.out.println(obj1.mathsMarks);
System.out.println(obj1.phyMarks);
//displaying marks for second object
System.out.println("Marks for second object:");
System.out.println(obj2.engMarks);
System.out.println(obj2.mathsMarks);
System.out.println(obj2.phyMarks);
}
}

c. Static variables in Java

We declare static keyword in java same as the instance variables, i.e. outside any object, block, or constructor, the difference being in java syntax that it uses the keyword static with it. It can also has as many copies which means it can be used for many classes. They create at start of program execution and destroy once the program ends.

Example

import java.io.*;
class Emp {
// static variable salary
public static double salary;
public static String name = "Harsh";
}
public class EmpDemo
{
public static void main(String args[]) {
//accessing static variable without object
Emp.salary = 1000;
System.out.println(Emp.name + "'s average salary:" + Emp.salary)
}
}

4.  Conclusion

In this tutorial for variables in Java, we studied the different types of java variables, Java variable declaration and uses of every variable type in java. In the upcoming tutorials, we will use these variable types and java methods to work on difficult programs and understand further topics better.

See Also – How to Convert Colored Image in Java

For reference