Java error: variable is already defined

Error reference

Java error: variable is already defined

This page is part of Intro to Java with Greenfoot. It assumes you have reached lesson 2.1. Opening it is not counted against you and is not tracked as progress.

The message

variable X is already defined in method Y

What it actually means

You declared the same variable name twice in the same place. The second declaration is trying to make a box that already exists.

The usual causes

Ranked by how often each one turns out to be the answer. Work down the list.

  1. Writing the type again on a later line: `int score = 0;` then `int score = 10;`.
  2. Two variables with the same name in the same method.
  3. A parameter and a local variable sharing a name.

How to fix it

Delete the type from the SECOND line. `int score = 0;` then `score = 10;` is correct. The type appears once, when the box is made.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]