-1

In my code inside the class, and outside the function. I have declared a Boolean variable which is set to true. And, I am using if statement on this variable. For example, if the given variable is true the do this else do something else.

But when I try to write code in eclipse. It gives me error stated below:

Syntax error on tokens, ConstructorHeaderName expected instead

I just simply tried to create a boolean variable and assigned true to it (As simple). And then created if statement and put if(x == true). Like this

class Test {
    boolean x = true;

    if(x == true){
        //code
    }
    else {
        //else another code
    }

    public static void partA(){
        //code
    }
}
  • 2
    the `if`/`else` will need to be inside a method. – Ousmane D. Dec 28 '18 at 08:44
  • You just check `if(x)`. And as Aomine indicated, it should be in the method. – Nikhil Dec 28 '18 at 08:46
  • Attributes can be outside of methods. Statements, e.g. your if-statements, need to be inside of methods. – elp Dec 28 '18 at 08:46
  • why you will do this? describe your need. you get this compile error because you cannt use if statement directly in your class. – Rasoul Taheri Dec 28 '18 at 09:50
  • Note: `boolean` the primitive and `Boolean` the wrapper class are not the same thing, and you are not using `Boolean` here. – Peter Lawrey Dec 28 '18 at 10:31
  • Possible duplicate of [Syntax error on tokens, ConstructorHeaderName expected instead & Syntax error on token "(", < expected](https://stackoverflow.com/questions/17352905/syntax-error-on-tokens-constructorheadername-expected-instead-syntax-error-on) – Joe Dec 28 '18 at 11:57

5 Answers5

2
if(x == true){
    //code
}
else {
    //else another code
}

This part should be inside one of the following,

  • Inside a method
  • Inside the main method
  • Inside a constructor
  • Inside an instance initializer block or in a static initializer block
Sandeepa
  • 3,457
  • 5
  • 25
  • 41
1

You are putting code directly inside of your class. The code needs to be inside of a method so it can be called and executed.

class Test {

   public static void partA(){
        boolean x = true;

        if(x == true){
              //code
        }
        else {
              //else another code
        }
   }
}

Then you can call it from another place like this:

Test.partA();

Reading this can be helpful.

Also, you don't need to do if(x == true). The if statement just checks that there's a true boolean inside it, so you could do if(x) and it'll do the same. Your way of doing it also works because x == true returns true if x contains the value true, but it's redundant code (You are actually telling Java: if (true == true)).

gbandres
  • 901
  • 7
  • 14
1

you can enclose your if else statement in a block like this:

{
    if(x == true){
        System.out.println("test");
    }
    else {

    }
}
DEVAS
  • 344
  • 2
  • 12
1

Always remember that statements should appear in a block of code.

You just need to put your if-else statement inside a method/static/block.

It will work perfectly fine.

See the example below:

class Test {
    static boolean x = true;

    static
    {
        {
            if (x) {
                // code
            } else {
                // else another code
            }
        }
    }

    {
        if (x) {
            // code
        } else {
            // else another code
        }
    }

    void m1()
    {
        {
            if (x) {
                // code
            } else {
                // else another code
            }
        }
    }

}

Please note :

I have used static variable just to use it inside a static block.

You can use if(x) instead of if (x == true)

sid_bond
  • 135
  • 3
  • 11
1

We have to write if statement inside a method/ static block/ instance block. if we use any variable inside static block then variable should be static but if we use variable in instance block then no need of static variable. see examples.

public class Test
{
//static block example
    static Boolean x;
    static
    {
      if(x)
      {
         System.out.println(x);
      }
     else 
     {

     }
   }

//instance block example
  Boolean x;
  {
        if(x)
        {
          System.out.println(x);
        }
        else 
        {

        }
     }
 //method example
    public void m()
    {
        Boolean x;
        if(x)
        {
           System.out.println(x);
        }
        else
        {
        }
    }
}