0

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);
        }
1
  • 1
    I suggest you learn about creating class instances and using instance fields. Commented Mar 17, 2020 at 19:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

The problem is - so it seems - he tries to use SizeSelect directly and doesn't instatiate it. That way it won't work. The more it looks like a return-statement would help more in this case.
@Melvin His attempt was to get amount directly from SizeSelect, this won't work, even with amount as a global variable and using a getter, because EnterNames should know from which instance of SizeSelect the amount has been initialized, and this is not possible unless EnterNames having a SizeSelect attribute which is set to the SizeSelect instance where the action has been handled and then sent back to EnterNames by setting its attribute with, for example : setSizeSelect(this). But this is clearly a bad design in my opinion.
@Melvin also, op doesn't provide enough information on how EnterNames is instantiated.
public class EnterNamesScene extends Scene { public EnterNamesScene(){ super(new EnterNames(), 1600, 900); } } That Is how EnterNames is instantiated
@TSoVz, I guessed correctly, so my solution should work for you :)
|
0

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

1 Comment

The amount field should probably not be static.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.