-2

If I have a constructor that holds a variable. How can I call that variable in another method within the same class?

In my example below, I want the code to return false in the checkBoundaries method if the col is greater that the Columnnum that is in the constructor.

    import java.util.Arrays;
    class MovieSeating 
    {
        private String[][] Seats;

        public MovieSeating(int rowNum, int columnNum)
        {
            Seats = new String[rowNum][columnNum];
            for (int r = 0; r < rowNum; r++)
            {
                for (int c = 0; c < columnNum; c++)
                {
                    Seats[r][c] = "?.?";
                    System.out.print(Seats[r][c] + " ");
                }
                System.out.println("\n");
            }

            System.out.println("Seating is empty.");



        }

        public boolean checkBoundaries(int row, int col)
        {
            if (col < 0 || row < 0 || col >= columnNumInConstructorParameter)
            {
                return false;
            }
            else {
                return true;
            }
        }
6
  • You mean a constructor with a parameter?
    – Math
    Nov 26, 2013 at 10:24
  • Your question does not make sense.
    – Adam Arold
    Nov 26, 2013 at 10:25
  • Ok let me rephrase in original.. sorry
    – Ryan
    Nov 26, 2013 at 10:25
  • The variable inside a constructor should be the instance or class variable. So that it can be accessed else where in the class. Do not declare a variable inside the constructor.
    – AJJ
    Nov 26, 2013 at 10:27
  • 2
    Your question is about scope of the variable. A method or a constructor are the same for scope of a variable. A method variable cannot be accessed from another method. Nov 26, 2013 at 10:28

6 Answers 6

3

You can use:

if (col < 0 || row < 0 || col >= Seats[0].length)

In general, if you want to use a variable inside a method, it must be set as an instance variable.

0
1

Make your variables into private instance members and call on them.

ex.

public class Foo {

    private int myVariable = 0;

    public Foo(int myVariable) {
        this.myVariable = myVariable;
    }

    public boolean positiveValue() {
        return this.myVariable > 0;
    }

}
1
class MovieSeating 
{
    private String[][] Seats;
    private int rowNumber = 0;
    private int columnNumber = 0;

    public MovieSeating(int rowNum, int columnNum)
    {
        this.rowNumber = rowNum;
        this.columnNumber = columnNum;
        Seats = new String[rowNum][columnNum];
        for (int r = 0; r < rowNum; r++)
        {
            for (int c = 0; c < columnNum; c++)
            {
                Seats[r][c] = "?.?";
                System.out.print(Seats[r][c] + " ");
            }
            System.out.println("\n");
        }

        System.out.println("Seating is empty.");



    }

    public boolean checkBoundaries(int row, int col)
    {
        if (col < 0 || row < 0 || row >= rowNumber || col >= columnNumber)
        {
            return false;
        }
        else {
            return true;
        }
    }
}
0

Just pass it by parameters

public Classname(){
    String foo = "bar";        
    myMethod(foo);
}
public void myMethod(String parameter){
    //do something
}
0

It's always a better practice to keep constructor parameters into instance variables.

0

It may be useful to store int rowNum, int columnNum as instance variable. However they are accessible from the size of the String[][] Seats variable

Consider adding something like:

private int getColumnNum() throws ArrayIndexOutOfBoundsException , NullPointerException{
    return Seats[0].length;
}
private int getRowNum(){
    return Seats.length;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.