Search code examples
javabooleanprogram-entry-point

Java - I need to use a variable boolean inside the main method, initialized outside but within the same class


The boolean has to be outside the main method so other methods can manipulate it. I've searched everywhere and cannot find a suitable answer because all I stumble upon are solutions for booleans as methods. It has to be a simple boolean and it cannot be static. Don't have much time, so any help would be great. Thanks.

public class myClass {

   private int[][] holdsStuff;
   private boolean isNeeded;

   public setFalse (){
   }

   public setTrue () {
   }

   public static void main(String[] args) {
      //call methods to change isNeeded
      //require isNeeded to prevent invalid changes being made to holdsStuff
   }
}

Solution

  • You want to use isNeeded in other methods and in main method which is static and static methods just deal with static data read this. so what you want to do is make instance of this class to call isNeeded in main method

     public static void main(String[] args) {
           myClass myclass = new myClass();          
           boolean isNeeded = myclass.isNeeded;
       }