Calling a global Array

14,495

Solution 1

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 :-)

Solution 2

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.

Solution 3

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.

Share:
14,495
Aaron Moodie
Author by

Aaron Moodie

Updated on June 04, 2022

Comments

  • Aaron Moodie
    Aaron Moodie almost 2 years

    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] = ' ';
                }
            }       
        }
    
    • bedwyr
      bedwyr almost 15 years
      Can you post a little more code? Is this homework? If so, would you tag it as such?
    • Aaron Moodie
      Aaron Moodie almost 15 years
      i've add the basic code. As mentioned, i'm just trying to call the array canvas in the method MakeNewCanvas. Canvas needs to stay as a public array, as i'll need to use and edit it in other methods in DrawingSystem
    • Luis Vito
      Luis Vito almost 15 years
      You didn't ask about it, but public static variables are not really an example of good design. You will run into all sorts of problems later on if you keep coding like that.
  • Aaron Moodie
    Aaron Moodie almost 15 years
    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
    Aaron Moodie almost 15 years
    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
  • pepe450
    pepe450 almost 15 years
    No problem, and there is nothing wrong with being new at something!
  • Code-Apprentice
    Code-Apprentice over 11 years
    "but as the class DrawingSystem isn't public..." This has nothing to do with whether DrawingSystem is public or not. The issues regarding the use of static apply to public classes as well as non-public ones.