简体   繁体   English

Java-我需要在main方法中使用变量boolean,在外部但在同一类中初始化

[英]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. 布尔值必须在main方法之外,以便其他方法可以对其进行操作。 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
   }
}

If class member isNeeded is not static, then it must belong to an instance of myclass , you can create a new instance and manipulate this instance: 如果类成员isNeeded不是静态的,则它必须属于myclass的实例,您可以创建一个新实例并操纵该实例:

public class myClass {

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

    public void setFalse (){
        isNeeded = false;
    }

    public void setTrue () {
        isNeeded = true;
    }

    public static void main(String[] args) {
        myClass mc = new myClass();
        myClass.setFalse();

    }
}
public class myClass {

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

       public void setFalse (){
       isNeeded =false;
       }

       public void setTrue () {
        isNeeded = true;
       }

       public static void main(String[] args) {

           myClass myclass = new myClass();
           myclass.setFalse();
           myclass.setTrue();
          //call methods to change isNeeded
          //require isNeeded to prevent invalid changes being made to holdsStuff
       }
    }

since main is static either use static keyward or make an instance of the same class. 由于main是静态的,所以请使用静态keyward或创建同一类的实例。 use static key the following: 使用静态密钥如下:

public class myClass {

   private int[][] holdsStuff; // make this static if you are also this inside main
   private static boolean isNeeded;

   public static setFalse (){
   }

   public static setTrue () {
   }

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

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 . 您想在其他方法和主要方法中使用isNeeded ,这是静态方法,而静态方法只是处理静态数据, 请阅读此内容 so what you want to do is make instance of this class to call isNeeded in main method 所以您要做的是在方法中使此类的实例调用isNeeded

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用方法内部定义的变量在Java中同一类的另一个方法中的方法外部使用 - How to use a variable defined inside a method use outside the method in another method on the same Class in java 访问在属于同一类 java 的方法中的构造函数中初始化/声明的变量 - Accessing a variable that is initialized/declared in the constructor within a method belonging to the same class java 为什么数组类型的类变量在声明后不能初始化,但是可以在Java中的方法内部初始化? - Why class variable of type array can't be initialized after declaration, but can be initialized inside a method in Java? 如何在同一个类(Java)中的另一个方法中引用一个方法中的变量? - How can I reference a variable within a method in another method in the same class (Java)? JAVA-尝试从类外部执行的操作中使用变量 - JAVA - Trying to use a variable from inside an actionperformed outside the class 主方法外的Java类实例 - Java class instance outside main method 在方法之外但在同一类中访问方法变量 - Accessing a method variable outside the method but in the same class 可以在 Java 中的类中初始化变量吗? 静态变量呢? 如果是,那么静态块有什么用? - Can a Variable be initialized inside a Class in Java? What about Static Variable? If yes, then what's the use for Static Block? Java - 如何在导入主类时使用方法中的变量 - Java - How to use a variable from a method when imported into the main class 如何在main方法中初始化变量,然后在初始化后使用它在屏幕上绘制图形 - How do I initialize a variable in the main method, and then use it to paint a graphic on screen AFTER it has been initialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM