Variables in java

Variables in Java

In the previous tutorial, you learnt about comments in Java.

In this tutorial, you’ll learn what variables are in Java, how they work, why we need them, naming conventions, how variables are represented in memory, best practices, and real-world usage, all explained in a simple and practical way.

What is Variable in Java?

A variable is like a container that stores data. Each variable should be given a unique name.   

Variables in Java (Conceptual Representation)

At any point during a program, you can access these containers (variables in Java) and access the data and you can also change it or use it to do something.

Updating a Variable in Java
				
					public class VariableInJava {
    public static void main(String[] args) {
        int a = 10;
        a = a + 20;
        System.out.println(a);
    }
}

				
			

How can we use variables in Java programs?

In Java, using a variable starts with declaring it. Once declared, we can assign a value to the variable either immediately or at a later point in the program. 

Using a variable in Java involves two main steps:

  1. Declaration 
  2. Initialization

1. Declaration

In Java, a variable must be declared before it can be used in a program.

Syntax for declaring a variable in Java:

				
					dataType variableName;
				
			
Example
				
					int bookCount;
				
			

In the above example, int is a data type in Java. A data type determines the kind of data a variable can store. We will explore data types in detail in an upcoming tutorial; for now, you only need to remember the basic syntax for declaring a variable in Java. 

bookCount is the name of the variable. Every variable must be given a unique name, also known as an identifier.

Local variables must be initialized before use. Otherwise, Java will throw a compile-time error.

				
					int bookCount;
System.out.println(bookCount); // compile-time error
				
			

2. Initialization

In Java, initialization is the step where a variable is given a value so it can be used in the program.Syntax for initializing a variable in Java:
				
					dataType variableName = initialValue;
				
			

(initialValue is optional at the time of declaration.)

Example
				
					int bookCount = 10;
				
			
In the above example, the variable bookCount is initialized with the value 10.
Variable Initialization
Or you can also do initialization later in the program like this:
				
					int bookCount;
bookCount = 10;

				
			

Note:

Java is a statically-typed language, which means you must declare the type of value a variable can hold before you use it.

				
					int bookCount = 10;
bookCount = "Musafir Cafe"; // error: incompatible types

				
			

This causes a compile-time error because bookCount is an int, and Java does not allow changing a variable’s data type.

Updating the variable

The value of a variable can be changed or can be updated in the program, That’s why it is called a variable.

For example:

				
					// initially, the person has 10 books
int bookCount = 10;

//now the person bought 10 more books
bookCount = bookCount + 10; //updating the initial bookCount

				
			

Naming convention for variables in Java

1. Java is case-sensitive: Hence, bookCount and BOOKCOUNT are two different variables.

For example:

				
					int bookCount = 10;
 int BOOKCOUNT = 30;

 System.out.println(bookCount); // prints 10
 System.out.println(BOOKCOUNT); // prints 30


				
			

2. Variables must start with either a letter, an underscore(_), or a dollar ($) sign.  

For example:

				
					int bookCount;  // valid name and good practice
 int _bookCount;  // valid but bad practice
 int $bookCount;  // valid but bad practice

				
			

3. Variable names cannot start with numbers.

For example:

				
					int 10Books;  // invalid variable name

				
			

4. Variable names can’t use whitespace.

For example:

				
					int book count;  // invalid: whitespace is not allowed in variable names

				
			

5. When creating variables, choose a name that makes sense. For example, age, userName, and userEmail make more sense than variable names such as a, u, and e.

6. For one-word variable names, use all lowercase letters. For example, it is better to use book rather than a BOOK or Book.

7. Variable name cannot be a keyword.

Variable representation in memory

In computers, everything is stored in memory. When we declare and initialize a variable like int bookCount = 10, the system assigns a memory location to the variable and stores the value 10 in that memory location.
Variable in memory

Summary

Let’s quickly recap:

  • Variables store data in Java programs.
  • Every variable must be declared before use.
  • Variables can be initialized during declaration or later.
  • Java follows strict naming rules for variables.
  • Understanding variables is essential for writing clean and maintainable code.

Frequently Asked Questions (FAQs)

What’s Next?

Now that you understand variables in Java, the next step is to learn data types in Java. 

In the next tutorial, we’ll learn about data types.

Java Tutorials

Stay Connected

Follow on Instagram for Java tips, coding reels, and much more.
Subscribe to VairagiCodes YouTube Channel for learning the art of programming.

If you liked this article, then share it with your friends.

Related Tutorials

introduction to java programming
Introduction to Java Programming
  • June 27, 2025
  • Com 0

Introduction to Programming Programming is a process to communicate with electronic devices like computers, phones, etc. As a beginner, you…

Getting started with Java
Getting Started with Java
  • July 1, 2025
  • Com 0

Getting Started with Java Programming Java is one of the most popular and powerful programming languages. Java is a high-level,…

Hello World in Java Write Your First Program
Hello World in Java: Write Your First Java Program
  • January 17, 2026
  • Com 0

Hello World in Java In the previous tutorial, you learned how to install Java on your computer. Now, let’s write your…

Comments in java
Comments in Java : A Complete Beginner’s Guide
  • February 4, 2026
  • Com 0

Comments in Java are just like notes in a textbook. Just like notes in a textbook help you understand a...

Variables in Java Featured Image
Variables in Java: How To Use Variables
  • February 10, 2026
  • Com 0

Variable is like a container that stores data. At any point during a program, you can open these containers(variables in...

Get Your Free Java
Data Structures Cheat Sheet! 🎁

Download the ultimate cheat sheet covering the Top 10 Data Structures every Java developer must know — with real-life examples! Just enter your email and get the free pdf.

We respect your privacy. No spam. Read our privacy policy for more information.