简体   繁体   English

布尔值在 Java 中未设置为 true

[英]boolean doesn't set to true in Java

Here am trying to change the status of 'stat' which isn't working我正在尝试更改不起作用的“stat”的状态

    public class controller {

        public static void main(String args[]){{

            final AjaxR re = new AjaxR();
            re.setMal("qw");
            if (re.getMal() != null) {
                re.setStat(true);
            }

            if(re.isStat()){
              System.out.println("Hello");
        }
    }



public class AjaxR {

    boolean stat;
    String mal;

    public boolean isStat() {
        return stat;
    }

    public void setStat(final boolean stat) {
        this.stat = stat;
    }

    public String getmal() {
        return mal;
    }

    public void setMal(final String mal) {
        this.mal = mal;

    }

Here re.stat isn't setting to true.这里re.stat没有设置为 true。 Unless I forcefully execute re.setStat(true) manually in debug mode, it is not changing.除非我在调试模式下手动强制执行re.setStat(true) ,否则它不会改变。

There are multiple syntactic problems with your code:您的代码存在多个语法问题:

  • Two opening { after your main method header;两个开头{在您的主要方法标题之后;
  • getMal() is the wrong case, it doesn't match your method getmal() in AjaxR ; getMal()是错误的情况,它与您在AjaxR中的方法getmal()不匹配;
  • There is no closing brace for your controller class;您的controller类没有右括号;
  • There is no closing brace for AjaxR either. AjaxR也没有AjaxR大括号。

However, when those errors are fixed (I will leave that as an exercise for you to complete!), your code behaves as you expect - "Hello" is printed and stat is definitely set to true.但是,当这些错误被修复后(我将把它作为练习留给您完成!),您的代码会按照您的预期运行 - 打印“Hello”并且stat绝对设置为 true。

I copied your code into eclipse and was able to easily find the rest of your errors.我将您的代码复制到 eclipse 中,并且能够轻松找到您的其余错误。 You did not have the correct amount of opening and closing braces, plus you named you method getmal() instead of getMal() .您没有正确数量的getmal()括号和getmal()括号,而且您将方法命名为getmal()而不是getMal() All these typos and errors can easily be detected if you use a proper IDE.如果您使用合适的 IDE,可以轻松检测到所有这些拼写错误和错误。

After inserting the correct amount of braces and fixing all the typos, the following code works and prints out Hello :插入正确数量的大括号并修复所有错别字后,以下代码有效并打印出Hello

public class controller {

    public static void main(String args[]){

        final AjaxR re = new AjaxR();
        re.setMal("qw");
        if (re.getMal() != null) {
            re.setStat(true);
        }

        if(re.isStat()){
            System.out.println("Hello");
        }
    }
}



public class AjaxR {

    boolean stat;
    String mal;

    public boolean isStat() {
        return stat;
    }

    public void setStat(final boolean stat) {
        this.stat = stat;
    }

    public String getMal() {
        return mal;
    }

    public void setMal(final String mal) {
        this.mal = mal;
    }
} 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM