🧰 Java Basics to Intermediate
Estimated reading: 3 minutes 273 views

Java Variables – Declare, Initialize & Use Variables in Java


Introduction – What Are Variables in Java?

Every Java program revolves around data — and variables are how we store and manipulate that data. Think of a variable as a container with a label, used to hold different kinds of information like numbers, text, or boolean values.

Whether you’re calculating tax or storing user input, understanding Java variables is a fundamental step.

By the end of this guide, you’ll be able to:

  • Declare and initialize variables
  • Understand Java’s variable types and naming rules
  • Use variables in expressions and output
  • Follow best practices for clean, readable code

What Is a Variable in Java?

A variable in Java is a named memory location that stores a value.

Syntax:

type variableName = value;

Example:

int age = 25;

Explanation:

  • int: data type (integer)
  • age: variable name
  • 25: value assigned to the variable

Types of Variables in Java

Java supports several types of variables, depending on where and how they are declared.

TypeScopeDefined InsideExample
LocalMethodMethod or blockint count = 10;
InstanceObjectClass (outside any method, without static)String name;
StaticClassClass with static keywordstatic int totalUsers;

Tip: Always initialize local variables before using them.


Declaring and Initializing Variables

Declare First, Assign Later:

int score;
score = 100;

Declare and Assign Together:

String city = "Mumbai";

Multiple Variables of Same Type:

int x = 10, y = 20, z = 30;

Warning: Avoid using undeclared or uninitialized variables — it causes a compile-time error.


Variable Naming Rules

Allowed:

  • Letters (a–z, A–Z)
  • Digits (0–9)
  • Underscore (_) and Dollar sign ($)

Not Allowed:

  • Names starting with a digit
  • Reserved keywords (class, int, etc.)

Examples:

Valid NamesInvalid Names
userName1user (starts with digit)
total_amountint (Java keyword)
MAX_SIZE@value (invalid symbol)

Best Practice: Use camelCase for variables (firstName, totalAmount).


Example: Using Variables in Java

public class UserProfile {
    public static void main(String[] args) {
        String name = "Alice";
        int age = 30;
        double height = 5.7;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Height: " + height + " feet");
    }
}

Output:

Name: Alice
Age: 30
Height: 5.7 feet

Explanation:

  • Different data types are used (String, int, double)
  • All values are printed using System.out.println()

Java Variable Data Types (Quick Overview)

TypeDescriptionExample Value
intInteger numbers100, -45
doubleDecimal numbers3.14, -9.8
charSingle character'A', '9'
booleanTrue/false valuetrue, false
StringSequence of characters"Hello"

You’ll explore these in detail in the Java Data Types section.


Summary – Java Variables Essentials

Java variables are the foundation of storing and manipulating data. You’ve learned:

  • How to declare and initialize variables
  • Types of variables: local, instance, static
  • Naming rules and best practices
  • How to use variables in Java programs.

FAQs – Java Variables

What is a variable in Java?

A variable is a container that holds a value — like a label on a storage box in memory.

Can a Java variable name start with a number?

No. Variable names cannot begin with digits, but digits can be used after the first character.

What happens if I don’t initialize a variable?

  • Local variables must be initialized before use — or you’ll get a compile error.
  • Instance variables get default values (0, false, null, etc.).

What’s the difference between String and char?

  • char: stores a single character ('A')
  • String: stores a sequence of characters ("Apple")

Share Now :
Share

Java Variables

Or Copy Link

CONTENTS
Scroll to Top