---
title: "Variable Declaration — AP Comp Sci A Definition & Guide"
description: "Variable declaration creates a variable by giving it a type and a name, like int count;. It's the Unit 1 foundation behind every line of AP CSA code you write."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/variable-declaration"
type: "key-term"
subject: "AP Computer Science A"
---

# Variable Declaration — AP Comp Sci A Definition & Guide

## Definition

In AP Computer Science A, a variable declaration creates a variable by stating its data type and name (like int score;), telling Java what kind of value the variable can hold and reserving memory for it before the variable is used anywhere in the program.

## What It Is

A variable declaration is how you bring a [variable](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") into existence in Java. You write the [data type](/ap-comp-sci-a/key-terms/data-type "fv-autolink") first, then the name: `int score;` or `double gpa;` or `String name;`. Java is a statically typed language, which means every variable's type is locked in at declaration. Once `score` is declared as an `int`, it can only ever hold int values. Try to stuff a `String` in there and the compiler refuses to run your code.

Declaration is just the birth certificate. It does not give the variable a value. That second step is initialization, and you can do both in one line (`int score = 0;`) or separately. In AP CSA you'll declare [local variables](/ap-comp-sci-a/unit-4/recursion/study-guide/p4D3YegZCLwQ3KJVvsd4 "fv-autolink") inside methods, parameters in method headers, and instance variables (usually marked `private`) inside classes. Same syntax everywhere, just different locations and lifetimes.

## Why It Matters

Variable declaration lives in [Unit 1](/ap-comp-sci-a/unit-1 "fv-autolink") ([Primitive Types](/ap-comp-sci-a/key-terms/primitive-types "fv-autolink")), where the CED expects you to declare variables of the correct type to represent primitive data like `int`, `double`, and `boolean`. It's arguably the first real Java skill the course builds, and everything else stacks on it. Reference variables for objects in Unit 2, parameter declarations in method signatures, and `private` instance variable declarations in Unit 5 all use the exact same type-then-name pattern. On the exam, sloppy declarations cost real points. An FRQ response that uses a variable without declaring it, or declares it with the wrong type, loses credit even if the logic is right. Choosing `int` when a calculation needs `double` is one of the most common self-inflicted wounds in AP CSA.

## Connections

### [Variable Initialization (Unit 1)](/ap-comp-sci-a/key-terms/variable-initialization)

Declaration creates the variable; [initialization](/ap-comp-sci-a/key-terms/initialization "fv-autolink") gives it its first value. Java lets you do both at once (int x = 5;), which is why so many people blur the two. On FRQs, the safest habit is to declare and initialize on the same line so you never use a variable that doesn't have a value yet.

### Data Types (Unit 1)

Every declaration starts with a type, so you literally cannot declare a variable without making a data-type decision. Picking int vs. double matters because [integer division](/ap-comp-sci-a/key-terms/integer-division "fv-autolink") truncates. Declare a variable as int when you need decimal precision and your average calculation silently drops everything after the decimal point.

### Scope (Unit 5)

Where you declare a variable decides where it exists. Declare it inside a for loop and it dies when the [loop](/ap-comp-sci-a/key-terms/loop "fv-autolink") ends; declare it as a private instance variable and it lives as long as the object does. Scope questions are really just questions about declaration location.

### [Scanner class (Unit 1)](/ap-comp-sci-a/key-terms/scanner-class)

Many intro classes use Scanner to read user input into declared variables, like int age = input.nextInt();. It's a nice way to practice matching declared types to incoming data, but heads up, Scanner itself isn't part of the AP Java subset, so the exam won't test you on it.

## On the AP Exam

You won't get an MCQ that asks "what is a variable declaration?" Instead, the exam tests whether you can read and write declarations correctly. Multiple-choice questions hand you code with declarations baked in and expect you to track each variable's type, especially when int vs. double changes the result of division. On the free-response section, declaring your variables is non-negotiable. Every FRQ where you write a method body or a class requires you to declare local variables with correct types and declare instance variables as private in class-design questions. Using an undeclared variable, redeclaring a parameter, or mismatching types are all errors the scoring guidelines penalize. The skill being graded is precise, compilable Java, and declarations are where that precision starts.

## Variable Declaration vs Variable Initialization

Declaration tells Java a variable exists and what type it is (int count;). Initialization assigns it its first value (count = 0;). The line int count = 0; does both at once, which is why the terms get tangled. The distinction matters because Java won't let you use a local variable that's been declared but never initialized; the compiler flags it as an error.

## Key Takeaways

- A variable declaration in Java states the data type followed by the variable name, like int score; and this must happen before the variable is used.
- Java is statically typed, so the type you declare is permanent; an int variable can never hold a String or a double.
- Declaration and initialization are different steps. Declaring creates the variable, initializing gives it a value, and int x = 5; does both in one line.
- Where you declare a variable controls its scope, so a variable declared inside a loop or method only exists there.
- On FRQs, you lose points for using variables you never declared or for declaring them with a type that breaks the math, like using int where decimal division is needed.
- In Unit 5 class-design questions, instance variables must be declared private to earn full credit.

## FAQs

### What is a variable declaration in AP Computer Science A?

It's the statement that creates a variable by giving it a data type and a name, like int count; or double price;. It tells Java what kind of value the variable holds and makes the name usable in your code.

### What's the difference between variable declaration and initialization?

Declaration creates the variable and fixes its type (int count;), while initialization assigns its first value (count = 0;). You can combine them as int count = 0;, which is the cleanest habit for FRQ code.

### Do I have to declare variables before using them in Java?

Yes. Java is statically typed, so every variable must be declared with a type before it's used, and the code won't compile otherwise. This is different from languages like Python, where you just assign a value and go.

### Can I change a variable's type after declaring it in Java?

No. Once you declare a variable as an int, it stays an int for its entire scope. You can change its value as many times as you want, but assigning a value of the wrong type causes a compiler error.

### Will I lose points on an AP CSA FRQ if I forget to declare a variable?

Yes, using an undeclared variable counts as an error under the scoring guidelines. Always declare local variables with the right type, and in class-design FRQs, declare instance variables as private.

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/variable-declaration#resource","name":"Variable Declaration — AP Comp Sci A Definition & Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/variable-declaration","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/variable-declaration#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T00:50:32.808Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/variable-declaration#term","name":"Variable Declaration","description":"In AP Computer Science A, a variable declaration creates a variable by stating its data type and name (like int score;), telling Java what kind of value the variable can hold and reserving memory for it before the variable is used anywhere in the program.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/variable-declaration","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a variable declaration in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"It's the statement that creates a variable by giving it a data type and a name, like int count; or double price;. It tells Java what kind of value the variable holds and makes the name usable in your code."}},{"@type":"Question","name":"What's the difference between variable declaration and initialization?","acceptedAnswer":{"@type":"Answer","text":"Declaration creates the variable and fixes its type (int count;), while initialization assigns its first value (count = 0;). You can combine them as int count = 0;, which is the cleanest habit for FRQ code."}},{"@type":"Question","name":"Do I have to declare variables before using them in Java?","acceptedAnswer":{"@type":"Answer","text":"Yes. Java is statically typed, so every variable must be declared with a type before it's used, and the code won't compile otherwise. This is different from languages like Python, where you just assign a value and go."}},{"@type":"Question","name":"Can I change a variable's type after declaring it in Java?","acceptedAnswer":{"@type":"Answer","text":"No. Once you declare a variable as an int, it stays an int for its entire scope. You can change its value as many times as you want, but assigning a value of the wrong type causes a compiler error."}},{"@type":"Question","name":"Will I lose points on an AP CSA FRQ if I forget to declare a variable?","acceptedAnswer":{"@type":"Answer","text":"Yes, using an undeclared variable counts as an error under the scoring guidelines. Always declare local variables with the right type, and in class-design FRQs, declare instance variables as private."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Variable Declaration"}]}]}
```
