views:

382

answers:

4

Hi,

I'm currently trying to draw shapes with 2D Arrays. In my class there is a global array defined with public char canvas[][];

Up until now, I have only declared arrays with char canvas[][] = new char[height][width];

If this Array has already been declared, and I'm not supposed to amend the code I've been given, how do I call an instance of that array so that I can use it?

thanks.

(edit)

class DrawingSystem {

    public char canvas[][];

       public static void makeNewCanvas(int tmpWidth, int tmpHeight) {

     canvas[][] = new char[tmpHeight][tmpWidth];

     for (int row=0; row<tmpHeight; row++) {
      for (int col=0; col<tmpWidth; col++) {
       canvas[row][col] = ' ';
      }
     }  
    }
A: 

Kinda confused about what you are asking for.

You can always redeclare the global array at any point, and use it for your own needs...but that seems rather suspect (meaning...why would this be a global array).

It seems like your canvas shouldn't be changing sizes...but than again, I don't know very much about your circumstances.

Check to see if the array is null, if so declare it to be the size that you want and use it happily?

bryansh
Hi, Thanks for the reply. The reason the canvas is global is so it can be modified by all the methods in the class. The first method defines it, second one draws a square in it and third prints it. The Array canvas has already been defined in the class as a public array, I'm just having trouble calling it in the methods, as if I just use canvas = new char[tmpHeight][tmpWidth]; inside the method, I get the error "on-static variable canvas cannot be referenced from a static context" when trying to complie.
Aaron Moodie
A: 

I'm not sure if I got your question right,

But looks like you need a Singleton pattern, instead of declaring the char canvas[][] as a public field, encapsulate the canvas array as read only property

public class MyDrawing 
{

private char canvas[][] = null;

public char[][] getCanvas()
{
   if (canvas!=null)
   {
      canvas =new char[height][width];
   }
   return canvas;
}

When use getCanvas() in other parts of your code when you need the canvas array instead of the previously used canvas public variable.

AB Kolan
+1  A: 

You have an incompatibility between static methods and instance variables.

Think about it this way: an instance variable is associated with a specific instance of a class; a static variable is associated with the class itself. You call static methods via the class:

ClassI.callStaticMethod();

Whereas you call an instance method via an instance of the class:

public ClassI classObj = new ClassI();
classObj.callInstanceMethod();

In the code you posted, there's an instance variable ("canvas") being set in a static method (main is associated with the Class, not an instance).

Therefore, you'll need to create instance methods to modify/update your "canvas", and create an instance of the class within the static function. This object (an "instance") can be used to update the instance variable.

Here's an example:

public class Foo {
    public char canvas[][];

    public static void main(String[] args) {
        Foo fooObj = new Foo(); //creates an instance of this class
        fooObj.createCanvas(2, 2);
        fooObj.modifyCanvas(0, 0, 'c');
    }

    public void createCanvas(int x, int y) {
        canvas = new char[x][y];
    }
    public void modifyCanvas(int x, int y, char c) {
        canvas[x][y] = c;
    }
}

This obviously isn't a one-to-one correlation to your assignment, but I'm sure you'll be able to adapt it to what you're doing :-)

bedwyr
Thanks bedwyr. With the example I posted, my class is being referenced by the Main method in another class (world) This code was provided, so all i'm supposed to do is create the methods in the class being called (ed, methods in DrawingObect). But I realised what its was. Up until now I'd only ever worked within one class, so was always using static to define my methods, but as the class DrawingSystem isn't public, I shouldn't be including static when defining my methods. Is that right? I removed static from the method name and it now complies.
Aaron Moodie
You're correct. The 'static' keyword is only to be used when you don't desire to maintain state using an instance of a class (e.g. a utility method which takes input and provides an output -- see Math.abs). In your particular case, static won't work with the instance variable you are working with. Good job!
bedwyr
Awesome! Thanks again!
Aaron Moodie
+1  A: 

Your problem is that makeNewCanvas(int tmpWidth, int tmpHeight) is static or public char canvas[][] is not static.

In Java static class members can only work with other static class members. Static members belong to the class and non static members belong to instances. The class is a template that is used to create objects, these objects are called instances of the class. When you declare something static it is shared by all instances of the class. In the case of methods this means that static methods must behave exactly the same on all instances.

DrawingSystem a = new DrawingSystem();
DrawingSystem b = new DrawingSystem();

a and b are instance of the class DrawingSystem that means they each have their own canvas array. Now since makeNewCanvas is static and must behave the same for all instances it cannot use a.canvas or b.canvas because they are unique to a and b and can have different contents.

Nash0
Thanks Nash, yeah, I realised what was going wrong after bedwyr posted his answer. As I mentioned above, I have only so far worked within one class, so this is the first time I've used objects in Java, hence was still calling the methods static ... Got it now though! Thanks again. A
Aaron Moodie
No problem, and there is nothing wrong with being new at something!
Nash0