(toc)


A variable in Java is a container for storing data that can be used and manipulated within a program. Variables have a name, a type, and a value. They are fundamental to Java programming as they allow you to store and manage data dynamically.


How to Declare a Variable in Java

To declare a variable, specify:

  1. The data type (e.g., int, String, boolean).
  2. The variable name (identifier).

Syntax:


data_type variable_name = value;

Example:


int age = 25; // Stores an integer value String name = "John"; // Stores a string boolean isAdult = true; // Stores a boolean value

Rules for Naming Variables

  1. Variable names must start with a letter, _, or $.
  2. Cannot use reserved keywords (like class, int).
  3. Should be descriptive and follow camelCase convention.
  4. Java is case-sensitive: myVar and MyVar are different.

Types of Variables in Java

  1. Local Variables
  2. Instance Variables
  3. Static (Class) Variables

1. Local Variables

  • Declared inside a method, constructor, or block.
  • Accessible only within the scope where declared.
  • Must be initialized before use.
Example:

public class Main { public static void main(String[] args) { int localVariable = 10; // Local variable System.out.println(localVariable); } }

2. Instance Variables

  • Declared outside methods, but inside the class.
  • Belong to an object, and each object has its own copy.
  • Automatically initialized to default values (e.g., 0 for integers).
Example:

public class Person { String name; // Instance variable int age; // Instance variable }

3. Static (Class) Variables

  • Declared with the keyword static.
  • Shared across all objects of the class.
  • Useful for constants or values shared among all instances.
Example:

public class Company { static String companyName = "TechCorp"; // Static variable }

Default Values for Variables

Java assigns default values to instance and static variables when no value is explicitly provided. Local variables do not get default values.

Data Type Default Value
byte, short, int, long 0
float, double 0.0
char ‘u0000’ (null char)
boolean false
String or other objects null

Variable Scope

The scope of a variable determines where it can be accessed in the program:

  1. Local Scope: Limited to the block or method where declared.
  2. Instance Scope: Accessible throughout the instance of the class.
  3. Static Scope: Shared and accessible by all instances of the class.

Examples of Variable Usage

Example 1: Local and Instance Variables


public class Person { String name; // Instance variable public void setName(String newName) { String greeting = "Hello"; // Local variable name = newName; System.out.println(greeting + ", " + name); } public static void main(String[] args) { Person person = new Person(); person.setName("Alice"); } }

Example 2: Static Variables


public class Counter { static int count = 0; // Static variable public Counter() { count++; } public static void main(String[] args) { new Counter(); new Counter(); System.out.println("Number of objects created: " + count); // Output: 2 } }

Final Notes

  1. Variables must be declared with a type before use.
  2. Choose appropriate variable types based on the data to optimize memory usage.
  3. Use meaningful variable names to enhance code readability.

Leave a Reply