File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Variable is not declared? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable is not declared?" Watch "Variable is not declared?" New topic

Variable is not declared?

Marco Segura

Joined: Oct 10, 2008
Posts: 3
I want to create a Flag class which will draw a flag and use it in a FlagComponent class to declare the draw positions and then see them in FlagViewer class

On my Flag class I get an error message when I declare my variables:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Line2D;

public class Flag
public Flag(int x, int y, int aWidth)
xLeft = x;
yTop = y;
width = aWidth;

"Cannot find symbol - variable width"

Why does this happen? Also, is it not possible to compile the class if the variables are "double" instead of "int"?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Marco,

Welcome to JavaRanch!

Before you can set the value of a member variable in a constructor like this, the variable has to be declared in the class body; that's where you specify the type of the variable, like this:

About the "double" thing: you can't assign a double value to an int variable without a castl perhaps that's the problem you're having. In other words, this is illegal:

double d = 2.0;
int i = 0
i = d; // Error

but this is OK:

double d = 2.0;
int i = 0
i = (int) d;

[Jess in Action][AskingGoodQuestions]
Marco Segura

Joined: Oct 10, 2008
Posts: 3
Oh I see, apparently I forgot to declare it, but I've done this type of coding while declaring those variables at the end of the code, for example:

But I guess both ways its ok.

Well, now I have another problem, as I try to draw a rectangle I get an error message saying "cannot find symbol - constructor Rectangle(int, int, double, double)", here is what I typed:

[ October 10, 2008: Message edited by: Marco Segura ]
Bert Deville

Joined: Oct 10, 2008
Posts: 8
The compiler has problems with the arguments you pass to the constructor call. Take a look at the API documentation for the Rectangle class. You'll notice that there are many overloaded constructors for this class, but none of them takes a combination of int, int, double and double as arguments.
Marco Segura

Joined: Oct 10, 2008
Posts: 3
Ok, I managed my way around that. One other question...

Like I mentioned on my first post, I have 3 classes, 1 is the FlagViewer, another is the FlagComponent and the last one is the Flag class which draws the flags.

I have the default flag code in Flag class with its respective colors. On my FlagComponent class I tell the program to draw 2 more flags on different coordinates... I want to know how to change the colors of the flags, where do i put the code? in the FlagComponent class or the Flag class?

This is my code edited of the Flag class (if you need it);

[ October 10, 2008: Message edited by: Marco Segura ]
I agree. Here's the link:
subject: Variable is not declared?
It's not a secret anymore!