BhauAutomation

Java Variables

In Java, variables are used to store data values. Each variable has a data type, a name, and stores a specific kind of value.

What are Variables in Java?

A variable in Java is a name assigned to a memory location that stores data. It allows a program to refer to values dynamically rather than using raw literals. Variables make code readable, reusable, and easier to maintain.

Example:
int age = 25;
String name = "John";
double salary = 45000.50;
System.out.println("Employee Name: " + name + ", Age: " + age);
    

Objectives of Using Variables

Variables help to store and manage data during program execution. They make code organized, enable calculations, and improve readability. Using variables ensures that data can be updated easily whenever needed.

Example:
int a = 5;
int b = 10;
int sum = a + b;
System.out.println("Sum: " + sum);
    

Types of Variables in Java

Local Variables: Declared inside a method and accessible only within that method.
Instance Variables: Declared inside a class but outside any method. Each object has its own copy.
Static Variables: Declared using the static keyword. Shared by all objects of the class.

Example:
public class TypesExample {
    int instanceVar = 100;        // Instance variable
    static int staticVar = 200;   // Static variable
    
    void show() {
        int localVar = 50;        // Local variable
        System.out.println("Local: " + localVar);
    }

    public static void main(String[] args) {
        TypesExample obj = new TypesExample();
        obj.show();
        System.out.println("Instance: " + obj.instanceVar);
        System.out.println("Static: " + staticVar);
    }
}
    

Rules for Declaring Variables

Variable names must start with a letter, underscore (_) or dollar sign ($). They cannot contain spaces or special symbols and must be declared before use. Java is case-sensitive, meaning age and Age are different identifiers.

Example:
int _count = 5;
int $value = 10;
int totalMarks = _count + $value;
System.out.println("Total: " + totalMarks);
    

Example: Declaring Variables in Java

public class VariablesExample {
    int instanceVar = 10;       // Instance variable
    static int staticVar = 20;  // Static variable
    
    void method() {
        int localVar = 30;      // Local variable
        System.out.println("Local Variable: " + localVar);
    }

    public static void main(String[] args) {
        VariablesExample obj = new VariablesExample();
        obj.method();
        System.out.println("Instance Variable: " + obj.instanceVar);
        System.out.println("Static Variable: " + staticVar);
    }
}
  

Best Practices

Use meaningful variable names such as studentName or totalPrice for better readability. Keep variable scope as small as possible, follow camelCase naming conventions, and always initialize variables before using them in your program.

Example:
String studentName = "Amit";
int totalMarks = 480;
System.out.println(studentName + " scored " + totalMarks + " marks.");