🟢 Foundation  ·  Lesson 05

Variables in Java

Variables in Java

What is a Variable?

A variable is a named container that stores a value in memory. In Java every variable must have a type.
int age = 18;
String name = "Aman";
double price = 99.5;

Types of Variables

TypeWhere Declared
Localinside a method, used only there
Instanceinside a class, one per object
Staticshared by all objects (uses static)

Example

public class Demo {
    static int count = 0;   // static
    int id;                 // instance
    void show() {
        int local = 5;      // local
        System.out.println(local);
    }
}

Summary

  • A variable stores a typed value.
  • Local (in method), instance (per object), static (shared).

Variable क्या है?

Variable एक named container है जो memory में value रखता है। Java में हर variable का एक type होना ज़रूरी है।
int age = 18;
String name = "Aman";
double price = 99.5;

Variables के प्रकार

Typeकहाँ declare
Localmethod के अंदर, सिर्फ वहीं
Instanceclass के अंदर, हर object का एक
Staticसभी objects में साझा (static)

Example

public class Demo {
    static int count = 0;   // static
    int id;                 // instance
    void show() {
        int local = 5;      // local
        System.out.println(local);
    }
}

सारांश

  • Variable typed value रखता है।
  • Local (method में), instance (हर object), static (साझा)।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n