Posts

Showing posts with the label Types of Variables in Java

Variables in Java Explained: Local, Static, and Instance

Variables in Java A variable is a symbolic name associated with a value. The value of a variable can change during program execution. In Java, variables are classified into three types: Local Variable Static Variable Instance Variable 1. Local Variable A local variable is declared inside a method, constructor, or block. Key points: Also called a method-level variable Accessible only within the block {} where it is declared Does not support default values Must be initialized before use If declared but not initialized and used, it causes a compile-time error If declared but not used, no error is generated Example : void display() {     int x = 10; // local variable     System.out.println(x); } 2. Static Variable A static variable is declared inside a class but outside any method, using the static keyword. Key points: Also called a class-level variable Supports default values Memory is allocated once and shared among all objects Can be accessed:     By variabl...