Java VariablesLast Updated : 2 Jan 2026 In Java, a variable is the name of memory location that are used like a container to store values during Java program execution. Every variable must be assigned a data type to define the kind of data it can hold. In Java, variables are categorized into three types: local, instance, and static variables. In this chapter, we will learn about the Java variables and types with the help of examples. Before understanding Java variables and their types, you must be familiar with what a variable is and how it is named. What is a Variable?A variable is the name of a reserved area allocated in memory. In other words, it is a name of the memory location. It is a combination of "vary + able" which means its value can be changed. ![]() Types of Java VariablesThere are three types of variables in Java:
![]() 1) Java Local VariableA variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists. A local variable cannot be defined with "static" keyword. Example of Local VariableLet us take an example to demonstrate the Local Variable in Java. ExampleCompile and RunOutput: Variable: 10 2) Java Instance VariableA variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static. It is called an instance variable because its value is instance-specific and is not shared among instances. Example of Instance VariableLet us take an example to demonstrate the Instance Variable in Java. ExampleCompile and RunOutput: Student Name is: Deepak Age: 19 3) Java Static VariableA variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class. Memory allocation for static variables happens only once when the class is loaded in the memory. Example Static VariableLet us take an example to demonstrate the Static Variable in Java. ExampleCompile and RunOutput: S1's age is: 23 S2's age is: 23 More Examples on Java VariablesLet’s see some other examples for better understanding about the Java variables: Variables Example 1: Add Two NumbersIn the following program, we use variables to store values and perform an addition operation. The variables a and b hold values, c stores their sum, and the result is printed. ExampleCompile and RunOutput: 20 Variables Example 2: Widening Type CastingThis example demonstrates implicit type casting in Java. The integer variable a stores the value 10, which is automatically converted to a float and assigned to f. Both values are then printed, showing that Java safely converts an int to a float without data loss. ExampleCompile and RunOutput: 10 10.0 Variables Example 3: Narrowing Type CastingThis example demonstrates explicit type casting from float to int in Java. Directly assigning f to a causes a compile-time error because a float cannot be automatically converted to an int. By using (int)f, the float value 10.5 is cast to 10, truncating the decimal part, and both values are printed. ExampleCompile and RunOutput: 10.5 10 Variables Example 4: OverflowThis example demonstrates overflow during explicit type casting in Java. The integer value 130 is explicitly cast to a byte, but since the byte range is from -128 to 127, the value overflows and results in -126. ExampleCompile and RunOutput: 130 -126 Variables Example 5: Adding Lower TypeThis example shows type promotion in Java. Although a and b are bytes, the expression a + b is automatically promoted to int, causing a compile-time error when assigned to a byte. By explicitly casting the result to byte, the value is stored in c and printed successfully. ExampleCompile and RunOutput: 20 Next TopicJava Data Types |
We request you to subscribe our newsletter for upcoming updates.