-6

Here am trying to change the status of 'stat' which isn't working

    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. Unless I forcefully execute re.setStat(true) manually in debug mode, it is not changing.

11
  • 3
    Please provide a complete and reproducible and compilable and well formatted example. Jun 11, 2015 at 15:51
  • There are numerous typos in here that prevent the code from compiling. If this is your actual class I'm not surprised it doesn't work. Jun 11, 2015 at 15:54
  • Um, your edit only introduced even more typos. Please take the time to make sure that your example actually represents your real code, or else you won't get any useful answers. In fact, I strongly suggest you use an IDE like Eclipse, Netbeans or IntelliJ, which catches problems like that for you and tells you how to fix them Jun 11, 2015 at 16:03
  • @All, I apologize for the typos. Can you check now?
    – knix2
    Jun 11, 2015 at 16:08

2 Answers 2

1

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;
  • There is no closing brace for your controller class;
  • There is no closing brace for AjaxR either.

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.

0

I copied your code into eclipse and was able to easily find the rest of your errors. You did not have the correct amount of opening and closing braces, plus you named you method getmal() instead of getMal(). All these typos and errors can easily be detected if you use a proper IDE.

After inserting the correct amount of braces and fixing all the typos, the following code works and prints out 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;
    }
} 

Your Answer

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

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