JAVA Tutorial



VARIABLES IN JAVA


🔸 Variables in Java

In Java, a variable is a container that holds data. Variables are used to store values that can be used and modified during the execution of a program. Each variable in Java must be declared with a specific data type, and the type of value it can hold depends on that data type. Variables must be initialized before they can be used in the program.

🔹 Declaring Variables

To declare a variable in Java, you need to specify the data type followed by the variable name. Here is the syntax:

// Syntax to declare a variable in Java
dataType variableName;
  

🔸 Example: Variable Declaration

public class VariableExample {
  public static void main(String[] args) {
    int age;            // Declare an integer variable
    double salary;      // Declare a double variable
    char grade;         // Declare a character variable

    age = 25;           // Initialize the variable 'age'
    salary = 50000.75;  // Initialize the variable 'salary'
    grade = 'A';        // Initialize the variable 'grade'

    System.out.println("Age: " + age);
    System.out.println("Salary: " + salary);
    System.out.println("Grade: " + grade);
  }
}
  

🔹 Initializing Variables

A variable can be declared and initialized at the same time. This is commonly done to set the initial value of the variable. Here is the syntax:

// Syntax for declaring and initializing a variable in Java
dataType variableName = initialValue;
  

🔸 Example: Variable Declaration and Initialization

public class VariableInitializationExample {
  public static void main(String[] args) {
    int age = 30;         // Declare and initialize the 'age' variable
    double salary = 55000.50; // Declare and initialize the 'salary' variable
    char grade = 'B';      // Declare and initialize the 'grade' variable

    System.out.println("Age: " + age);
    System.out.println("Salary: " + salary);
    System.out.println("Grade: " + grade);
  }
}
  

🔸 Types of Variables in Java

There are three types of variables in Java:

  • Local Variables: These are declared inside a method or block and can only be accessed within that method or block.
  • Instance Variables: These are declared within a class but outside any method. They represent the state of an object and are associated with instances of a class.
  • Class Variables (Static Variables): These are declared with the static keyword and belong to the class rather than instances of the class. They can be accessed without creating an object of the class.

🔸 Example: Local, Instance, and Static Variables

public class VariableTypesExample {

  // Instance variable
  int instanceVar = 10;

  // Static variable
  static int staticVar = 20;

  public void display() {
    // Local variable
    int localVar = 30;

    System.out.println("Local Variable: " + localVar);
    System.out.println("Instance Variable: " + instanceVar);
    System.out.println("Static Variable: " + staticVar);
  }

  public static void main(String[] args) {
    VariableTypesExample obj = new VariableTypesExample();
    obj.display();
  }
}
  

🔸 Key Points to Remember

  • Java variables must be declared with a specific data type.
  • Variables can be declared and initialized at the same time.
  • There are three types of variables: Local, Instance, and Static.
  • Instance variables are associated with an object, whereas static variables belong to the class itself.
💡 Tip: Always initialize your variables before using them. In Java, uninitialized local variables will result in a compile-time error.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review