1

Here is my first class called World

public class World {

    private static char[][] world2D;
    private int characterRow;
    private int characterColumn;


    public World(int width, int height){
        world2D = new char[width][height];
        characterColumn = 0;
        characterRow = 0;

        for(int i = 0; i < world2D.length; i++){
            for(int j = 0; j < world2D[i].length; j++){
                world2D[i][j] = '-';
            }
        }

        world2D[characterRow][characterColumn] = 'P';
    }

    public void moveUp(){
        world2D[characterRow][characterColumn] = '-';
        if (characterRow > 0){
            characterRow -= 1;
        }
        world2D[characterRow][characterColumn] = 'P';
    }

    public void moveDown(){
        world2D[characterRow][characterColumn] = '-';
        if (characterRow < world2D.length){
            characterRow += 1;
        }
        world2D[characterRow][characterColumn] = 'P';
    }

    public void moveRight(){
        world2D[characterRow][characterColumn] = '-';
        if (characterColumn < (world2D[characterRow].length - 1)){
            characterColumn += 1;
        }
        world2D[characterRow][characterColumn] = 'P';
    }

    public void moveLeft(){
        world2D[characterRow][characterColumn] = '-';
        if (characterColumn > 0){
            characterColumn -= 1;
        }
        world2D[characterRow][characterColumn] = 'P';
    }

    public static void displayWorld(){
        for(int i = 0; i < world2D.length; i++){
            for(int j = 0; j < world2D[i].length; j++){
                System.out.print(world2D[i][j]);
            }
            System.out.println();
        }       
    }

}

Here is my second class called Driver

import java.util.Scanner;

public class Driver {
    public static void main(String[]args){
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        System.out.print("How tall should the world be?: ");
        int height = input.nextInt();
        System.out.print("How wide should the world be?: ");
        int width = input.nextInt();

        World myWorld = new World(width,height);
        World.displayWorld();
    }

}

Why don't I need to call displayWorld specifically on the myWorld instance of the World class?

What if I created multiple World instances? This can't be right.

**edit for more detail

I want to call one of the class methods (i.e. moveUp or moveDown) on the instance of the World class myWorld object. However, I can't pass my reference to that object (myWorld) into those methods. I want to be able to call one of those methods, which changes the postion of 'P' in the 2 dimensional array and print it out using the methods that I have defined including the displayWorld method

Efie
  • 1,430
  • 2
  • 14
  • 34
  • Hi, I didn't get a clear idea of what your problem was. Would you care to illustrate it in more detail? – theguywhodreams May 01 '15 at 02:12
  • I want to call one of the class methods (i.e. moveUp or moveDown) on the instance of the World class myWorld object. However, I can't pass my reference to that object (myWorld) into those methods. I want to be able to call one of those methods, which changes the postion of 'P' in the 2 dimensional array and print it out using the methods that I have defined including the displayWorld method – Efie May 01 '15 at 02:16
  • 1
    TAsk no need to be condescending.. I'm asking because I am new to programming and don't understand why it isn't working. If you aren't here to help or explain please leave – Efie May 01 '15 at 02:17
  • 1
    No worries! `static` types are pretty confusing when you're new. You'll get the hang of it. :) Posted a link in my answer, you might want to check it out. – theguywhodreams May 01 '15 at 02:19
  • Thank you that definitely cleared it up. I'm using Eclipse and it told me to change those values to static values. I think I was referencing the instance of myWorld incorrectly. Thank you for your help :] – Efie May 01 '15 at 02:29

2 Answers2

1

Static variables, such as private static char[][] world2D, exist within a class, not objects of that class. This means they are accessed through the class name, not an instance name. Since you are initializing it in your constructor, it will be changed each time you create a new world, but the value will be the same for each instance. It looks like you should make the variable world2D non-static (just remove the static keyword) and do the same on the function displayWorld() Then, the line World.displayWorld(); can be replaced with myWorld.displayWorld();

Vincent
  • 484
  • 3
  • 9
1

Please refer to this link to learn more about static types.

If you want to display worlds of different instances, remove static from public static void displayWorld() and call myWorld.displayWorld().

Community
  • 1
  • 1
theguywhodreams
  • 315
  • 1
  • 8