Woong-Sup Jung
Woong-Sup Jung

Reputation: 2337

Java, using a variable within classes

How can I use the variable drawMax in a different class?

public int DrawNumberSetting (int drawMax)
{
    System.out.print ("Please enter the maximum number of draws you want to play: ");
    drawMax = scan.nextInt ();
    return drawMax;
}

Upvotes: 0

Views: 108

Answers (2)

javamonkey79
javamonkey79

Reputation: 17775

You can turn drawMax into a class level variable and provide a getter\accessor method, like this:

private int drawMax;

public void DrawNumberSetting ()
{
    System.out.print ("Please enter the maximum number of draws you want to play: ");
    drawMax = scan.nextInt ();
}

public int getDrawMax()
{
   return drawMax;
}

Upvotes: 0

willcodejavaforfood
willcodejavaforfood

Reputation: 44063

Lets say your drawNumberSetting(int drawMax) is declared in class A. That means that any other class that has an instance of class A can call that method and use the returned value.

class B
{
    public void my otherMethod()
    {
        A a = new A();
        int drawMax = a.drawNumberSetting(5);
    }
}

Upvotes: 1

Related Questions