Blnpwr
Blnpwr

Reputation: 1875

How to access an array from another class

I want to operate on the array called "players" that is declared in the main method. I want to use "players" in my class called "Glucksspielthread" I know that I can't access "players" because it is declared in the main method and is not visible for other classes.

How can I solve this problem? Here is my code:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Glucksspieltest {
    public static void main(String[] args) {
        int numPlayers = Integer.parseInt(args[0]);
        int threadSize = Integer.parseInt(args[1]);

        ExecutorService es = Executors.newFixedThreadPool(threadSize);
        Glucksspielthread[] players = new Glucksspielthread[numPlayers];
        for (int i = 0; i < numPlayers; i++) {
            players[i] = new Glucksspielthread(i);
            es.execute(players[i]);
        }
    }
}

class Thinker {
    public static void think(int Millisekunden) {
        try {
            Thread.sleep(Millisekunden);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void randomThink(int minMillisekunden, int      maxMillisekunden) {
        System.out.println("test");
    }
}

class Glucksspielthread implements Runnable {
    public int playerNumber;

    Glucksspielthread(int number) {
            playerNumber = number;
    }

    @Override
    public void run() {
        for (int i = 0; i <= playerNumber; i++) {
            // here, I want to operate on array called "players" that is   declared in the main method  
        }
    }
}

Upvotes: 1

Views: 10374

Answers (3)

Arvind Gangwar
Arvind Gangwar

Reputation: 153

Make players as private global referance variable

public class Glucksspieltest {
//Make a Global reference variable players
   private static Glucksspielthread[] players; 

// Make a getter Method to get players
 public static Glucksspielthread[] getPlayers(){
    return players;
}

    public static void main(String[] args) {
        int numPlayers = Integer.parseInt(args[0]);
        int threadSize = Integer.parseInt(args[1]);

        ExecutorService es = Executors.newFixedThreadPool(threadSize);

        players = new Glucksspielthread[numPlayers];
        for (int i = 0; i < numPlayers; i++) {
            players[i] = new Glucksspielthread(i);
            es.execute(players[i]);
        }
    }
}

And access it by Glucksspieltest.getPlayers();

class Glucksspielthread implements Runnable {
    public int playerNumber;

     private static Glucksspielthread[] players;

    Glucksspielthread(int number) {
            playerNumber = number;
    }

    @Override
    public void run() {
        for (int i = 0; i <= playerNumber; i++) {
            // here, I want to operate on array called "players" that is declared in the main method  
         players= Glucksspieltest.getPlayers();  // play with players
        }
    }
}

Upvotes: 0

aniski
aniski

Reputation: 1271

Add a method to class Glucksspieltest, and make the players array global:

public class Glucksspieltest {
    private static Glucksspielthread[] players;

    public static Glucksspielthread[] getPlayers(){
        return players;
    }

    public static void main(String[] args) {
        int numPlayers = Integer.parseInt(args[0]);
        int threadSize = Integer.parseInt(args[1]);

        ExecutorService es = Executors.newFixedThreadPool(threadSize);
        players = new Glucksspielthread[numPlayers];
        for (int i = 0; i < numPlayers; i++) {
            players[i] = new Glucksspielthread(i);
            es.execute(players[i]);
        }
    }
}

This way you can get the array by calling the getPlayers() method.

(Note that, it would be adviced to add a constructor to initialize and fill the players array, and separate the player management from the main method as well.)

Upvotes: 1

dumitru
dumitru

Reputation: 2108

Just for your test purpose make your players variable static and public in the Glucksspieltest class, like this:

public class Glucksspieltest {

public static Glucksspielthread[] players;

Then acces it in the Glucksspielthread class like this:

for (int i = 0; i <= playerNumber; i++) {
        // here, I want to operate on array called "players" that is   declared in the main method
        Glucksspieltest.players
    }

Upvotes: 2

Related Questions