OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Variables in Java

 

What is a Variable in Java?

A variable in Java is a named memory location that stores data values of a specific type. It acts as a container for values that can change during program execution.


Variable Declaration and Initialization

Syntax:


type variableName; // Declaration type variableName = value; // Declaration + Initialization

Example:


int age; // Declaration age = 25; // Initialization String name = "Loki"; // Declaration and Initialization

Types of Variables in Java

Java variables are classified based on their scope and position in the program:

TypeDeclared InsideScopeMemory Location
Local VariableMethod or blockWithin method/block onlyStack
Instance VariableInside class (outside methods)Whole object (per instance)Heap
Static VariableInside class using staticShared across all instancesMethod Area (static memory)

1. Local Variable

  • Exists only within methods, constructors, or blocks.

  • Must be initialized before use.


public class Test { public void display() { int localVar = 10; // Local variable System.out.println(localVar); } }

2.  Instance Variable (Non-static Field)

  • Declared in a class but outside any method.

  • Each object gets its own copy.


public class Student { String name; // Instance variable int rollNo; void showDetails() { System.out.println(name + " " + rollNo); } }

3.  Static Variable (Class Variable)

  • Declared with static keyword.

  • Shared among all instances of the class.


public class Student { int rollNo; static String college = "ABC College"; // Static variable Student(int r) { rollNo = r; } void display() { System.out.println(rollNo + " " + college); } }

Variable Naming Rules and Conventions

Rules (enforced by compiler):

  • Must begin with a letter, $, or _.

  • Cannot start with a digit.

  • No spaces or special characters (except $ and _).

  • Can't use Java keywords (like int, class).

Conventions (good practices):

Element TypeNaming StyleExample
VariablecamelCasestudentName
ConstantUPPER_CASEMAX_SPEED

Data Types for Variables

Java is strongly typed, so every variable must have a type.

Primitive Types:

  • Integer types: byte, short, int, long

  • Floating types: float, double

  • Character: char

  • Boolean: boolean


int age = 25; float marks = 95.5f; char grade = 'A'; boolean isPassed = true;

Reference Types:

  • Arrays

  • Objects

  • Strings

  • Interfaces


String name = "Loki"; int[] scores = {90, 85, 95};

Variable Scope and Lifetime

TypeScopeLifetime
LocalMethod/block onlyDuring method execution
InstanceWhole class (per object)As long as object exists
StaticClass-wideTill end of program

Example Program Showing All Types


public class Demo { // Static variable static int staticCount = 0; // Instance variable int instanceCount = 0; public void showCounts() { // Local variable int localCount = 10; System.out.println("Local: " + localCount); System.out.println("Instance: " + instanceCount); System.out.println("Static: " + staticCount); } public static void main(String[] args) { Demo obj1 = new Demo(); obj1.instanceCount = 1; staticCount = 5; obj1.showCounts(); } }

 Best Practices for Variable Usage

  • Use meaningful names (balance, not b).

  • Initialize variables before using them.

  • Keep variable scope as small as possible.

  • Use constants (final) for fixed values.


final int MAX_USERS = 100;

Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC