Arc
Arc

Reputation: 449

Accessing method from different class

I'm trying to call a class's void from a different class. The objects work fine, but for some reason its not completing the action.

I'm making a black jack game, and I need some button to appear when the user enters a correct bet. Here is what I have:

 public static void showButtons(Boolean x) { // to show or hide the buttons
            if(x) {
                hitButton.setVisible(true);
                standButton.setVisible(true);
                separator.setVisible(true);
            }
            else {
                hitButton.setVisible(false);
                standButton.setVisible(false);
                separator.setVisible(false);

           }
}

Once the bet is verified as an integer, it passes through this:

private void bdButtonActionPerformed(java.awt.event.ActionEvent evt) { //bdButton is the bet button
        ...
        if(validBet(s)) {
            bet = Integer.parseInt(s); // parses input string into an int
            System.out.println("bet accepted"); // debug to know if it works this far
            NewRound nr = new NewRound();
            System.out.println("created object");
            nr.newHand(bet);
            //showButtons(true); // this works, it changes the buttons, but only from here
        }
        else {
            ...
        }
    }

Here is the newHand method:

public static void newHand(int bet) {
        System.out.println("In newHand"); // debug
        BlackJack b = new BlackJack();

        b.showButtons(true);
        System.out.println("passed showButtons"); // the code gets to here, but buttons are still not visible
    }

Upvotes: 0

Views: 69

Answers (2)

Zavior
Zavior

Reputation: 6462

The method is declared static, so assuming it is a class called TestClass, the way you would call the method looks like this:

TestClass.newHand(int bet);

If you want to be able to just call

newHand(int bet);

in your current class, you would need to use static import, like this:

import static your.package.TestClass.newHand;

But I would much prefer having it the first way.

Upvotes: 1

rafalio
rafalio

Reputation: 3946

Your newHand method is static, so it should be called with the class name.

Upvotes: 1

Related Questions