aspose file tools*
The moose likes Beginning Java and the fly likes undefined variable! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "undefined variable!" Watch "undefined variable!" New topic
Author

undefined variable!

Fahed Ghazali
Greenhorn

Joined: May 06, 2001
Posts: 4
hi there...
here is some of my code:

when I compile this... it says that the Variable "Width" in my method "slowShow()" is undefined.
how do I get around solving this? I cant define width as an instance variable as it comes into play only after the constructo uses the argument int[][] pixels.
pls help me out yall!
cheers

[This message has been edited by Fahed Ghazali (edited May 13, 2001).]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Can't you just define width as an instance variable of the class i.e. int width;
then in the constructor initialize it to it's appropriate value
i.e width = initialValue;

Bosun

Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Bosun is correct, the problem is that you have declared and initialized the width (and height) variable inside your constructor, and it is therefore local to that constructor. Once it is over the rest of the code can't see it.
To prevent this, declare the variable outside the constructor, and just initialized it in the constructor. This way the methods will be able to access it later.
You are also going to have a problem with you GrayScaleCanvas because you declared it as an instance variable and then REDECLARED it in the constructor - which created a DIFFERENT variable with the same name but local to the method. When you initialize gsc do NOT redeclare it.

[This message has been edited by Cindy Glass (edited May 13, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Fahed Ghazali
Greenhorn

Joined: May 06, 2001
Posts: 4
Thanx... appreciate the help guys!!
now another slight problem!
in my original code right... I have also initialised the argument int[][]pixels in the constructor... coz thass where Im gonna get the information from. But I need to use the 2d array pixels an all my other methods. How do I declare this as an instance variable?
do I just add
int[][]pixels as an instance variable n then in the constructor what do I do to make it equal to the int[][]pixels in the argument??

this might seem trivial to yall...... but Im still learning ppl!
cheers
thanx
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Put
this.pixels = pixels ;
in the constructor.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: undefined variable!