Java Variables Explained with Simple Examples

Java variables are containers that store data values. Every program relies on variables to process information.

1. Types of Variables in Java

Java supports three types of variables:

  • Local Variables – Declared inside methods
  • Instance Variables – Part of object state
  • Static Variables – Shared by all objects

2. Example

public class Demo {
int age = 22; // Instance variable
static String city = “Hyderabad”; // Static variable

void show() {
    int marks = 85;    // Local variable
    System.out.println(marks);
}

}

3. Quick Tips

  • Always initialize your variables.
  • Use meaningful names.
  • Use camelCase for naming.

Leave a Comment

Your email address will not be published. Required fields are marked *