简体   繁体   中英

Sharing Variables between classes in Java

I have to create a maze game in my class. I've created the maze, but I need to share the variables from the main class to the other classes (it is a requirement).

Here is my main code:

import java.io.*;
import java.util.Scanner;

public class MazeGame 
{


    public static void main(String[] args) 
    {
        getVariables();  
    }

    static void getVariables()
    {
        String fileName = "C:\\Users\\baileyjstewart\\Documents\\NetBeansProjects\\MazeGame\\src\\Maze.input";
        int size;
        int row;
        int column;
        String line = null;

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            Scanner s = new Scanner(new File("C:\\Users\\baileyjstewart\\Documents\\NetBeansProjects\\MazeGame\\src\\Maze.input"));

            int[][] array = new int[size = s.nextInt()][size];
            for (row = 0; row < size; row++)
                {
                   for(column = 0; column < size; column++)
                   {
                       array[row][column] = s.nextInt();
                       if(array[row][column] == 0)
                       {
                           System.out.print("  ");
                           //System.out.print(array [row][column] + " ");
                       }
                       else if(array[row][column] == 1)
                       {
                           System.out.print("X "); 
                       }
                       else if(array[row][column] == 2)
                       {
                           System.out.print("E ");
                       }
                   }   
                   System.out.println(" ");
                }

            // Always close files.
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '");

        }

    }
}

And I'm trying to share the variables row, column, and size into my Location class:

import java.io.*;
import java.util.Scanner;

public class Location 
{

    MazeGame Location = new MazeGame();


    public Location()
    {
        String Wall;
        String Space;
        String Endpoint;
        Boolean Visited;
        Boolean hereNow;   
    }

    public void getVariables()
    {
        Location.getVariables();
        if(row == 0)
        {

        }

    }



}

Thank you in advance for any help.

public class Location 
{

MazeGame Location = new MazeGame();
....

This wont work because the class name is exactly the same as the variable name.

Also, your Location class is never used because the program entry point ( main function ) is declared in the MazeGame class.

I wrote another answer elsewhere that goes into explaining constructors, classes, and setters/getters that you may want to read.

Let me know if you need more examples or if you still don't understand.

As others have pointed out, the class name 'Location' is same as the variable (object) name. You need to read about OOP rules and principles.

One way you to share variables across classes will be to declare the variable as 'static'. Please remember that 'static' variables are not encouraged and they are considered as evil.

Still, if you would like to see how your code can share variables across classes, here is your code with some changes (though this is NOT perfect).

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MazeGame 
{

    protected static int row;

    public static void main(String[] args) throws IOException 
    {
        getVariables();  
    }

    public int getRow() {
        return MazeGame.row;
    }

    static void getVariables() throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        System.out.println("Enter a value for row : ");
        String strRow = br.readLine();
        MazeGame.row = Integer.parseInt(strRow);

        Location ln = new Location();
        ln.getVariables();
    }
}


import java.io.IOException;

public class Location 
{

     MazeGame Locationx = new MazeGame();

    public Location()
    {

    }

    public void getVariables() throws IOException
    {
         System.out.println("The value of class variable row, initially is: "+MazeGame.row); // class variable

         Locationx.row = 21; // object variable
         System.out.println("The value of object variable row, after modification is: "+Locationx.row);

         MazeGame.row = 23; // class variable
         System.out.println("The value of class variable row, after modification is: "+MazeGame.row);
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM