A variable in Java is a named memory location that stores a typed value during program execution. Java has three variable types: local, instance, and static. As of Java 17, the language reserves 53 keywords that cannot be used as variable names. Every variable must be declared with a data type before use.
- Key Takeaway 1: Java has three types of variables: local, instance, and static. Each has a different scope and lifetime.
- Key Takeaway 2: Java has 53 reserved keywords as of Java 17. You cannot use any of them as variable names.
- Key Takeaway 3: Variable names in Java are case-sensitive. age, Age, and AGE are three different variables.
- Key Takeaway 4: Static variables are shared across all instances of a class. Instance variables belong to one object. Local variables exist only inside a method.
- Key Takeaway 5: Strong fundamentals in Java variables and keywords are tested in placement exams like TCS NQT, AMCAT, and eLitmus, and in university exams across BTech, BCA, and MCA programmes at institutions affiliated with Anna University, VTU, and SPPU.
What Variables Are in Java
A Java variable definition at its simplest is this: a variable is a symbolic name tied to a memory location that holds a value. When you write int marks = 95;, the JVM allocates memory for an integer, labels it marks, and stores 95 there. You can read it, update it, or pass it to a method.
Every variable declaration in Java follows the same structure: data type, variable name, optional initialisation. For example, String studentName = "Priya"; declares a String variable called studentName and initialises it immediately. You can also declare without initialising, like int rollNumber;, but you must assign a value before you use it or the compiler will throw an error.
Java is a statically typed language, which means types are checked at compile time, not runtime. This catches a huge class of bugs early. According to the Stack Overflow Developer Survey 2024, Java remains among the top five most-used programming languages professionally, which makes understanding its variable system genuinely career-relevant, not just exam material.
If you’re coming from C, the declaration syntax will feel familiar. Our guide on how to write, compile and run a C program walks through a similar compile-time discipline that Java inherits and extends.
Primitive vs Reference Variables
Java variables fall into two broad categories before you even think about scope. Primitive variables hold actual values directly. Reference variables hold a memory address pointing to an object, like a String, an array, or any class instance.
The eight primitive types, their storage size, and their value ranges are worth knowing for placement MCQs and university exams:
| Primitive Type | Size (bits) | Default Value | Value Range |
|---|---|---|---|
| byte | 8 | 0 | -128 to 127 |
| short | 16 | 0 | -32,768 to 32,767 |
| int | 32 | 0 | -2,147,483,648 to 2,147,483,647 |
| long | 64 | 0L | -9.2 x 10^18 to 9.2 x 10^18 |
| float | 32 | 0.0f | ~3.4 x 10^-38 to ~3.4 x 10^38 |
| double | 64 | 0.0d | ~1.7 x 10^-308 to ~1.7 x 10^308 |
| char | 16 | ‘\u0000’ | 0 to 65,535 (Unicode) |
| boolean | 1 (JVM-dependent) | false | true or false |
This distinction matters in practice. When you pass a primitive to a method, Java copies the value. When you pass a reference type, the method can modify the original object. Getting this wrong is one of the most common sources of subtle bugs in beginner Java code.
Types of Variables in Java
The three types of variables in Java are defined by where you declare them and how long they live. Each behaves differently, and mixing them up causes real problems in code.
Local Variables
A local variable is declared inside a method, constructor, or block. It exists only while that method is executing and is destroyed when the method returns. Local variables have no default value, so the compiler will refuse to compile your code if you try to read one before assigning it.
Example: inside a method calculateGrade(), you might write int total = marks1 + marks2;. That total variable is local. Nothing outside that method can see it.
Instance Variables
An instance variable is declared inside a class but outside any method. Each object of the class gets its own copy. If you create two Student objects, each has its own studentName, its own rollNumber, and its own examMarks. Changing one object’s instance variable doesn’t touch the other’s.
Instance variables are given default values automatically: integers default to 0, booleans to false, and object references to null. That’s different from local variables, and it trips up a lot of beginners who assume the same rule applies everywhere.
Static Variables
A static variable is declared with the static keyword inside a class. There is exactly one copy of it, shared by every instance of that class. If you change a static variable in one object, every other object sees the new value immediately.
A common real-world use: tracking how many Student objects have been created with a static counter like static int studentCount = 0;. Every time a new object is constructed, you increment it, and the count is accurate regardless of which object you ask.
Quick Comparison
| Variable Type | Declared Inside | Default Value | Shared Across Objects? | Lifetime |
|---|---|---|---|---|
| Local | Method / block | None (must assign) | No | Method execution |
| Instance | Class body | Yes (type-dependent) | No | Object lifetime |
| Static | Class body (static) | Yes (type-dependent) | Yes | Program lifetime |
Understanding these distinctions helps you avoid Java exceptions and common runtime errors like NullPointerException, which often trace back to uninitialised instance or reference variables.
Java Keywords: The Full Reserved List
Java keywords are reserved words that the language itself uses. You cannot use them as variable names, class names, or method names. According to the Oracle Java Language Specification, Java 17 defines exactly 53 reserved keywords, up from 50 in earlier versions with the addition of sealed, permits, and record. Trying to name a variable class or int will give you a compile-time error instantly.
Types of Keywords in Java by Category
The types of keyword in Java can be grouped by function. This makes them easier to remember for exams and interviews.
| Category | Keywords |
|---|---|
| Data Types | int, float, double, char, boolean, byte, short, long |
| Access Modifiers | public, private, protected |
| Class & Object | class, new, this, super, instanceof, extends, implements, interface, abstract, enum, record, sealed, permits |
| Control Flow | if, else, switch, case, default, for, while, do, break, continue, return |
| Exception Handling | try, catch, finally, throw, throws |
| Modifiers | static, final, native, synchronized, transient, volatile, strictfp |
| Package & Import | package, import |
| Other | void, null, true, false, assert, var |
Two words, goto and const, are reserved but not actually used in Java. They exist to prevent programmers from using names that might confuse compilers or future versions of the language. This is a detail that comes up in university exams and in technical interviews at product companies.
The Use of Keywords in Java: Practical Examples
The use of keywords in Java is constant, even in the shortest program. When you write public static void main(String[] args), you’re using four keywords in a single line: public, static, void, and technically String is a class, not a keyword, which is a distinction worth knowing for MCQ-style exams.
The keyword final makes a variable constant. Once you write final int MAX_STUDENTS = 60;, you cannot change that value later. The keyword static lifts a variable from instance scope to class scope. The keyword var, introduced in Java 10, lets the compiler infer the type: var courseName = "Ethical Hacking"; is valid, and the compiler treats it as a String.
If you’re planning a career in software, knowing these keywords cold is a baseline expectation. Our article on software development tools to learn first covers the broader toolkit you’ll need beyond language fundamentals.
Naming Rules for Variables in Java
Java enforces specific rules for variable names, and breaking them causes a compile error. These aren’t style preferences; they’re part of the language specification.
Hard Rules (Compiler Enforced)
- Variable names must start with a letter, an underscore (_), or a dollar sign ($). They cannot start with a digit.
- After the first character, you can use letters, digits, underscores, or dollar signs.
- You cannot use a Java keyword as a variable name.
- Variable names are case-sensitive. totalMarks and TotalMarks are different variables.
- There is no official maximum length, but most style guides cap at 50 characters for readability.
Conventions (Community Standard)
- Use camelCase for variable and method names: studentAge, examMarks, rollNumber.
- Use UPPER_SNAKE_CASE for constants declared with final: MAX_RETRIES, PI_VALUE.
- Class names use PascalCase: StudentRecord, CourseManager.
- Keep names meaningful. s is a terrible variable name. studentScore is clear and searchable in a codebase.
One practical tip from real development work: when you’re debugging a 3,000-line Java file before a deadline, descriptive variable names are the difference between finding a bug in five minutes and spending an hour. Name your variables for the next person reading the code, even if that person is future you.
Students preparing for placements at IT firms or building skills for a career as a full stack developer vs software developer will find that code readability is assessed in technical interviews just as much as correctness.
Common Naming Mistakes to Avoid
A surprisingly common mistake is using a keyword accidentally, especially class or int, because they look like natural English words in some contexts. Another frequent error is starting a variable name with a number, like 1stRank, which the compiler rejects immediately. The dollar sign is technically legal but strongly discouraged by convention; you’ll mostly see it in auto-generated code.
If you’re coming from a Python background, remember that Java doesn’t allow you to start a variable with a lowercase letter and then switch to uppercase mid-word without following camelCase. Consistency matters because Java projects in enterprise environments often have hundreds of classes, and naming chaos makes them unmaintainable.
For students in BSc Computer Science subjects and scope programmes or those exploring jobs and salary after MCA, Java is one of the most exam-tested and job-relevant languages you’ll study. According to NASSCOM’s 2023 Tech Talent Report, Java is among the top three languages demanded by Indian IT employers for fresher roles, with Java developer salaries for freshers in India ranging from Rs 3.5 LPA to Rs 6 LPA depending on the company and city. Getting variables and keywords right is non-negotiable.
According to the TIOBE Index for 2024, Java consistently ranks in the top three programming languages by popularity globally, confirming that time spent mastering its fundamentals has a long shelf life in the job market.
Frequently Asked Questions
What are variables in Java?
A variable in Java is a named memory location that stores a value of a specific data type. You declare it with a type and a name, like int age = 21;, and the Java compiler enforces that only values of that type are stored there. Variables must be declared before use and can be updated during program execution unless marked final.
What are the types of variables in Java?
Java has three types of variables: local variables (declared inside a method, exist only during that method’s execution), instance variables (declared in a class body, each object gets its own copy), and static variables (declared with the static keyword, shared across all instances of a class). Each type has different scope, lifetime, and default value behaviour.
What are Java keywords?
Java keywords are reserved words that the language specification defines for its own use. You cannot use them as identifiers, variable names, or class names. Examples include int, class, static, public, if, for, and return. They form the core grammar of the Java language and are checked at compile time.
How many keywords does Java have?
Java has 53 reserved keywords as of Java 17, according to the Oracle Java Language Specification. This includes newer additions like sealed, permits, and record introduced in recent versions. Two words, goto and const, are reserved but unused in the language.
What are the variable naming rules in Java?
Variable names in Java must start with a letter, underscore, or dollar sign, never a digit. They are case-sensitive, cannot be a reserved keyword, and can include letters, digits, underscores, and dollar signs after the first character. By convention, use camelCase for variable names and UPPER_SNAKE_CASE for constants. The compiler enforces the hard rules; conventions are enforced by code review.
What is the difference between instance and static variables in Java?
An instance variable belongs to a specific object — each object created from a class has its own separate copy. A static variable belongs to the class itself and is shared by all objects. If you update a static variable through one object, every other object sees the updated value immediately. Instance variables are used for object-specific data; static variables are used for class-wide data like counters or constants.
You now have a complete, exam-ready picture of variables in Java and Java keywords. The three variable types, the 53 reserved words, and the naming rules are the foundation every Java programme builds on, whether you’re writing a student grade calculator or a production microservice.
- This week: Write a small Java class with at least one local, one instance, and one static variable. Compile and run it. Observe which variables are accessible where.
- Next: Study Java exception handling to understand what happens at runtime when variables are null or out of bounds. Our guide on Java exceptions and common runtime errors is a natural next step.
- Career move: If you want to apply Java fundamentals in cybersecurity, secure coding, or ethical hacking, explore 3.0 University online certification courses. Programming knowledge is the direct foundation for ethical hacking, AI development, and blockchain work. The CEH v13 AI online course at 3.0 University is designed for exactly this transition, from coder to certified security professional.
Last updated: June 2025. Reviewed by the 3University editorial team.