Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 05

Variables in Java

In this lesson: Learn Java variables — local, instance, static — declaration rules and naming with examples and output. In Hindi & English.

What is a variable

A variable is a named box in memory that holds a value. In Java, every variable has a fixed type that you state when you declare it — Java is statically typed. This means you always know what kind of value a variable can hold, which makes programs safer.

💡 In Java you must give the type: int age = 18; — the type int is required.

Declaring variables

int    age   = 18;        // whole number
double price = 99.5;      // decimal number
char   grade = 'A';       // single character
boolean pass = true;      // true / false
String name  = "Aman";    // text

System.out.println(name + " is " + age);
Aman is 18

The pattern is: type, name, =, value, then ;. Unlike loosely typed languages, you must always write the type in Java.

Common types

TypeHoldsExample
intWhole numbers18
doubleDecimal numbers99.5
charOne character'A'
booleantrue / falsetrue
StringText"Aman"

int, double, char and boolean are primitive types. String is a class for text, written with a capital S.

Naming rules

  • Start with a letter, underscore or dollar sign — not a number.
  • After that, letters, numbers and underscores are allowed.
  • No spaces and no Java keywords (like int or class).
  • Case-sensitive: age and Age are different.
  • By convention, use camelCase: totalMarks, not total_marks.

Local vs instance variables

public class Student {
    String name;              // instance variable (per object)

    void show() {
        int marks = 90;       // local variable (inside method)
        System.out.println(name + " " + marks);
    }
}

An instance variable (like name) belongs to each object and lasts as long as it. A local variable (like marks) exists only inside its method and disappears when the method ends.

Constants with final

final int MAX_USERS = 100;
// MAX_USERS = 200;   // ERROR: cannot change a final

Use final to make a variable a constant that cannot change after it is set. By convention, constant names are written in UPPERCASE, like MAX_USERS.

Common mistakes

  • Forgetting the type. Java needs it: int age = 18;, not age = 18;.
  • Wrong type for the value. Putting 99.5 into an int loses the decimals or errors.
  • Case slips. age and Age are different variables.
  • Using a variable before assigning it. Local variables must be given a value before use.

Summary

  • A variable stores a value and, in Java, has a fixed type.
  • Declare with type, name, value: int age = 18;.
  • Common types: int, double, char, boolean, String.
  • Local variables live in methods; instance variables belong to objects.
  • Use final for constants that cannot change.

Next, explore all types in Java data types, or work with numbers using Java operators.

Frequently Asked Questions

How do I declare a variable in Java?
You write the type, then the name, then a value — for example int age = 18;. Unlike some languages, Java is statically typed, so you must state the type when you declare a variable.
Why must I declare the type in Java?
Java is statically typed, meaning every variable has a fixed type known at compile time. This catches type errors early and makes programs safer and faster. So int x can only hold whole numbers.
What is the difference between int and double?
int stores whole numbers like 18, while double stores decimal numbers like 99.5. Use int for counts and IDs, and double for prices, averages and measurements.
What is the difference between a local and an instance variable?
A local variable is declared inside a method and exists only while that method runs. An instance variable is declared in a class (outside methods) and belongs to each object, lasting as long as the object.
How do I make a constant in Java?
Use the final keyword: final int MAX = 100;. Once set, a final variable cannot be changed. Constants are usually written in uppercase by convention.
← Back to Java Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicData Types in Java Your progress: 5 / 61 (8%) · Lesson 6 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free