Ugenz Academy/Tutorial

Variables

6 min read1,041 words

Reading progress

Keep learning

Variables in Java

Introduction

A variable is a named memory location used to store data in a Java program. The value stored in a variable can be changed during program execution.

For example:

int age = 20;

Here:

  • int → Data type
  • age → Variable name
  • 20 → Value

Syntax of a Variable

dataType variableName = value;

Example:

int age = 20; String name = "Anuradha"; double salary = 25000.50;

Why Are Variables Used?

Variables are used to:

  • Store data
  • Reuse data in a program
  • Perform calculations
  • Store user input
  • Make programs easier to understand and maintain

Types of Variables in Java

Java has three main types of variables:

  1. Local Variable
  2. Instance Variable
  3. Static Variable

1. Local Variable

A variable declared inside a method, constructor, or block is called a local variable.

Example:

public class Main { public static void main(String[] args) { int age = 20; System.out.println(age); } }

Here, age is a local variable because it is declared inside the main() method.

Characteristics

  • Declared inside a method, constructor, or block.
  • Can only be accessed within that method or block.
  • Must be initialized before it is used.

2. Instance Variable

A variable declared inside a class but outside methods, constructors, or blocks is called an instance variable.

Example:

class Student { String name; int age; void display() { System.out.println(name); System.out.println(age); } }

Here, name and age are instance variables.

They belong to an object of the Student class.

Using Instance Variables

public class Main { public static void main(String[] args) { Student s1 = new Student(); s1.name = "Anuradha"; s1.age = 20; s1.display(); } }

3. Static Variable

A variable declared using the static keyword is called a static variable or class variable.

Example:

class Student { String name; static String college = "Rajarshi Janak University"; }

The static variable college is shared by all objects of the class.

Example

class Student { String name; static String college = "Rajarshi Janak University"; void display() { System.out.println(name); System.out.println(college); } } public class Main { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student(); s1.name = "Anuradha"; s2.name = "Ram"; s1.display(); s2.display(); } }

Data Types of Variables

Variables must have a data type.

Primitive Data Types

Java has eight primitive data types:

Data TypeExample
bytebyte age = 20;
shortshort number = 1000;
intint age = 20;
longlong population = 100000L;
floatfloat price = 10.5f;
doubledouble salary = 25000.50;
charchar grade = 'A';
booleanboolean passed = true;

Non-Primitive Variables

Examples include:

  • String
  • Arrays
  • Classes
  • Objects
  • Interfaces

Example:

String name = "Anuradha";

Declaring a Variable

A variable can be declared without assigning a value.

int age;

The value can be assigned later:

age = 20;

Initializing a Variable

Initializing means assigning a value to a variable.

int age = 20;

Here, age is declared and initialized at the same time.

Changing a Variable's Value

The value of a variable can be changed.

int age = 20; age = 21; System.out.println(age);

Output:

21

Multiple Variables

Multiple variables of the same type can be declared together.

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

However, declaring variables separately is often easier to read:

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

Constants

A variable whose value cannot be changed after initialization can be declared using the final keyword.

Example:

final double PI = 3.14159;

Trying to change it will cause an error:

PI = 3.14; // Error

By convention, constants are written in uppercase letters.

final int MAX_SIZE = 100;

Variable Naming Rules

Java has rules for naming variables.

Valid Names

age studentName student_name age2 $total

Invalid Names

2age // Cannot start with a number student-name // Hyphen is not allowed class // Reserved keyword

Naming Conventions

Java commonly uses camelCase for variable names.

Good examples:

studentName totalMarks phoneNumber dateOfBirth

Avoid:

studentname StudentName student_name

The last two may be valid Java identifiers, but they do not follow the usual Java camelCase convention for variables.

Variable Scope

Scope means the part of the program where a variable can be accessed.

Example:

public class Main { public static void main(String[] args) { int age = 20; if (age >= 18) { System.out.println(age); } } }

The variable age can be accessed inside its surrounding method.

A variable declared inside a smaller block may only be available inside that block.

if (true) { int number = 10; System.out.println(number); } // System.out.println(number); // Error

Complete Example

public class Main { static String college = "Rajarshi Janak University"; String name; int age; public static void main(String[] args) { int year = 2026; Main student = new Main(); student.name = "Anuradha"; student.age = 20; System.out.println("Name: " + student.name); System.out.println("Age: " + student.age); System.out.println("College: " + college); System.out.println("Year: " + year); } }

Output:

Name: Anuradha Age: 20 College: Rajarshi Janak University Year: 2026

Summary

TypeDeclared WhereBelongs To
Local VariableInside method/blockMethod or block
Instance VariableInside class, outside methodsObject
Static VariableInside class with staticClass

Conclusion

Variables are an essential part of Java programming. They allow programs to store, access, modify, and process data. The three main types of variables are local, instance, and static variables. Understanding their declaration, scope, data types, and usage is important for learning Java.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy