Calling the global array
I am currently trying to draw shapes using 2D arrays. There is a global array in my class defined withpublic char canvas[][];
So far, I've only been declaring arrays with char canvas[][] = new char[height][width];
If this array is already declared and I don't have to fix the code I was given, how can I call an instance of this 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 source to share
You have an incompatibility between static methods and instance variables.
Think of 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 through the class:
ClassI.callStaticMethod();
While you are calling an instance method through an instance of the class:
public ClassI classObj = new ClassI();
classObj.callInstanceMethod();
In the code you posted, there is an instance variable ("canvas") set in a static method ( main
bound to the class, not the instance).
Therefore, you need to create instance methods to modify / update your "canvas" and create an instance of the class in a static function. This object ("instance") can be used to update an 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 is obviously not a one-to-one correlation with your assignment, but I'm sure you can adapt it to what you are doing :-)
a source to share
Your problem is what makeNewCanvas(int tmpWidth, int tmpHeight)
is static or public char canvas[][]
not static.
In Java, static members of a class can only work with other static members of a class. Static members belong to the class and non-static members belong to instances. A class is a template that is used to create objects, these objects are called class instances. 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 the same for all instances.
DrawingSystem a = new DrawingSystem();
DrawingSystem b = new DrawingSystem();
a
and b
are instances of the class DrawingSystem
, which means that each has its own array canvas
. Now, since it 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 content.
a source to share
I'm not sure if I got your question right,
But it looks like you want the Singleton pattern, instead of declaring it char canvas[][]
as a public field, encapsulate the canvas array as a read-only property
public class MyDrawing
{
private char canvas[][] = null;
public char[][] getCanvas()
{
if (canvas!=null)
{
canvas =new char[height][width];
}
return canvas;
}
When used getCanvas()
elsewhere in your code, when you need a canvas array instead of the previously used public variable.
a source to share
Scanty about what you ask for.
You can always update the global array at any time and use it for your own needs ... but that seems rather suspicious (which means ... why it would be a global array).
It seems like your canvas shouldn't be resizing ... but the more I don't really understand your circumstances.
Check if the array is null if so declared to be the size you want and use it happily?
a source to share