0

I am trying to access a boolean variable stay from my class MontyHall but cannot because it is non-static and i am trying to access it in a static context Here is the code:

public void updateStatistics(Door door1, Door door2, Door door3)
{
    this.numGames = this.numGames + 1;
    oneDoor(door1, 0);
    oneDoor(door2, 1);
    oneDoor(door3, 2);

    if (MontyHall.stay == true){
        this.numStay = this.numStay + 1;
    }
    else{
        this.numSwitch = this.numSwitch + 1;
    }
}

The variable stay is located in class MontyHall. Any help would be greatly appreciated as I am very confused how to fix this Properties of class MontyHall:

public class MontyHall {
  boolean stay;
  Door A = new Door("A");
  Door B = new Door("B");
  Door C = new Door("C");

  public MontyHall(Door a, Door b, Door c){
    this.A = a;
    this.B = b;
    this.C = c;
  }}
user207421
  • 305,947
  • 44
  • 307
  • 483

4 Answers4

1

Your code MontyHall.stay is the part where you are trying to reference it statically (by using the class name).

A non-static field will need an instantiated object in order to reference. In this case, if this method is within MontyHall, then you can use use this.stay in order to access it, instead of MontyHall.stay. If the method you've listed above is not within the MontyHall class then you will need to create a new MontyHall object like such: MontyHall montyHall = new MontyHall();

Alternatively, you may want to make your stay variable static, in which case just add a simple static keyword to the variable declaration.

1

am trying to access a boolean variable stay from my class MontyHall but cannot because it is non-static and i am trying to access it in a static context

Everything is clear now. Your variable stay is an instance variable which belongs to each individual object of class MontyHall. So you shouldn't contemplate making it static just to resolve the error.

To access an instance variable, you need to create an object first. (Since it belongs to a particular object and not a class):

MontyHall hall = new MontyHall();
hall.stay;    //access stay from object of MontyHall

In the rule of data protection and encapsulation, you may consider making stay variable private and use getters and setters to access it.

So if you set stay as private, you will access it like this:

hall.getStay();

Last but not least, Java do not have global variables. It is a rather common misconception to perceive class variables (static variables) as global.

user3437460
  • 17,253
  • 15
  • 58
  • 106
0

You can't just access variables from non-static contexts. You will have to instantiate the class first and then access bariable from that instance. So it would go something like this:

MontyHall a = MontyHall();
a.someVariable....

Note: variable should be accessible from other classes (would recommend encapsulation depending on situation).

More info in this thread: calling non-static method in static method in Java

Community
  • 1
  • 1
Jaskaranbir Singh
  • 2,034
  • 3
  • 17
  • 33
-1

You want to access a variable directly from the class, without creating an instance. To do so, the variable should be declared static. In that class, you should thus define it as follows:

public class MontyHall {
    public static boolean stay = true;
    .....
}

Now you can access it in the way you did.

Edit

Or, as the others have mentioned as well. If stay is a variable that can change within your class, you should make an instance first.

MontyHall a = new MontyHall(....);
a.stay; // This is the stay variable from MontyHall a
MontyHall b = new MontyHall(....);
b.stay; // This stay variable could have a different value