简体   繁体   中英

How Would I get access to a variable from a different class in java?

I currently have a variable that I would like to access in a different class but i'm not sure on how I would go about doing so. I have tried doing stuff like int nums = SizeSelect.amount; but that has not worked. Here is the code from the class I'm trying to access the amount varaible.

public class SizeSelect extends BorderPane {
    public SizeSelect(){
        size8.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            int amount = 9;
        });

        size32.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            int amount = 33;
        });

        size16.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            int amount = 17;
        });

and here is the code that I'm trying to access that amount varible

public class EnterNames extends BorderPane {
    public EnterNames(){
        int nums = SizeSelect.amount;
        for (int j = 0; j < nums; j++) {
            int teamNum = j + 1;
            TextField test = new TextField();
            test.setPromptText("Enter Team " + teamNum);
            box.getChildren().add(test);
            test.setMaxWidth(500);
        }

Your question needs some information on when and where EnterNames is declared, but assuming it is done in EnterNamesScenes , then one solution would be to first add amount as a EnterNamesScenes attribute, and then pass directly the value from it as a constructor argument (in SizeSelect ) and then use it to construct a EnterNames instance.

For example :

size16.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene(17));
}

And in EnterNamesScene :

private int amount;
public EnterNamesScene(int amount){
    this.amount=amount; //this value comes from the button action and will be saved to create a EnterName

    //Constructor stuff
}

Then whenever you want to create a EnterNames instance, simply add the value of amount as an argument :

EnterNames en = new EnterNames(this.amount);

And the constructor :

public EnterNames(int amount){
    for (int j = 0; j < amount; j++) {
    int teamNum = j + 1;
    // Rest of the code
}

NOTE : As I said before, this will only work in the case where EnterNames is created inside EnterNamesScenes . Otherwise, you will have to provide more information about how EnterNames is instantiated.

The amount variable in SizeSelect is local to each onClicked handler, and thus not reachable outside the handler function scope.

If you want to be able to reach SizeSelect.amount, you need to declare it as static in the class.

public class SizeSelect extends BorderPane {
    public static int amount;

    public SizeSelect(){
        size8.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            amount = 9;
        });

        size32.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            amount = 33;
        });

        size16.setOnMouseClicked(e ->{
            MainScene.mainStage.setScene(new EnterNamesScene());
            amount = 17;
        });

Note that this is a quick fix, the more appropriate solution would be to use a getter/setter. See https://www.w3schools.com/java/java_encapsulation.asp

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