Master Java Variables in 2025: Types, var Keyword Simplified & Top Interview Questions

 

Java Variables Explained: Types, var Keyword & Interview Questions for Beginners


What is a Variable in Java?

To fully understand how variables work in Java, it’s helpful to first know how Java runs your programs. This involves understanding key Java components like the JDK, JRE, and JVM, which are responsible for compiling and executing your code.

A variable is a name or label that stores data in your program’s memory. Think of it as a container or box holding information such as numbers, text, or true/false values.


How to Declare Variables in Java

Variables must be declared before use, specifying their data type and name:


in above diagram there is a variable x
which contain interger type of value i.e 20

java
dataType variableName = value;


Example:

java

int age = 25; String name = "Alice"; boolean isStudent = true;

You can also declare without initialization:

java

int marks; marks = 90;

Types of Variables:

there are 3 types of variables which explain below
Types of variable.


1. Local Variables

Declared inside methods and accessible only within that method. Must be initialized before use.

2. Instance Variables/ Global Variables

Declared inside a class but outside methods. Each object of the class has its own copy.

3. Static Variables

Declared with the static keyword and shared across all objects of the class.


Primitive Data Types in Java

TypeExample
intint age = 20;
floatfloat price = 9.99f;
doubledouble pi = 3.14;
charchar grade = 'A';
booleanboolean flag = true;

Type Inference with var

Starting with Java 10, you can use var to let Java automatically determine the variable’s type based on the assigned value.

Instead of:

java

int age = 25; String name = "Alice";

You can write:

java

var age = 25; var name = "Alice";

Important points about var:

  • Must assign a value when declaring with var.

  • Can only be used for local variables inside methods.

  • The inferred type is fixed and checked by Java at compile time.


Default Values of Variables

Variable TypeDefault Value
int0
booleanfalse
char'\u0000'
Object typesnull

Note: Local variables do NOT have default values and must be initialized before use.


Simple Example Combining Variables

java

public class VariableExample { static String company = "OpenAI"; // static variable String name = "Alice"; // instance variable public void showDetails() { int age = 25; // local variable System.out.println(name + " is " + age + " years old, works at " + company); } public static void main(String[] args) { VariableExample v = new VariableExample(); v.showDetails(); } }

Best Practices for Using Variables

  • Use meaningful names like userAge instead of x.

  • Follow camelCase naming convention.

  • Initialize variables before use.

  • Limit the variable scope to where it is needed.


Common Java Variables Interview Questions

  1. What is a variable in Java?

  2. What are the different types of variables?

  3. What is the default value of a boolean variable?

  4. Can local variables have default values?

  5. What is the var keyword in Java?

  6. What is variable shadowing?

  7. Where are local variables stored in memory?

  8. Can you use static variables inside methods?


Conclusion

Variables are a fundamental part of Java programming. Understanding how to declare and use different types of variables — including the modern var keyword — will improve your coding skills and prepare you for technical interviews. Practice writing code using variables to get comfortable and confident!


๐Ÿ’ฌ Join the Conversation!

Do you have any questions or want to share your experience with Java variables? Maybe you found a tricky interview question or a cool coding trick? Drop your thoughts below — I’d love to hear from you! ๐Ÿ‘‡


Comments Section

Comments