user3297076
user3297076

Reputation: 85

How to break inside if condition in java

i Have below method . which i am calling an boolean method inside it.if that boolean method is true then exit from the block else continue with else block. Below is what i tried. but failed to break the method.

public IStatus validate() throws ExecutionValidationException {
    ConfigurationHandler handler = new ConfigurationHandler(getExecutionParameters());
    if (handler.isModelValidationEnabled()){
      //how to handle here and exit here. i need to continue the application
    } else
        this.loggerStorage.getLogger().info(VALIDATION_DM_START);
    try {
        return getDataModel().validateModel();
    } finally {
        this.loggerStorage.getLogger().info(VALIDATION_DM_FINISHED);
    }
}

Upvotes: 0

Views: 8842

Answers (4)

kaushik
kaushik

Reputation: 312

public void method1(){
    A a = new A();
    if(a.method2() == false){
        continue with method3();
    }
}

Upvotes: 0

davidxxx
davidxxx

Reputation: 131396

i need to exit from method1()completely. i don't want to continue else part.How can i break/exit here... and continue with the application. If false then execute else part

It is the goal of a if else block.

Here if the if statement is true, the else block is never reached.

if(a.method2() == true){
   ...
}
else{
   ...
}

You could use a return statement if you had some other processings after the if statement that you don't want ignore in this specific case. But in this case you don't need to couple the if where you want to make a return with other else if blocks as they are not dependent:

if(a.method2() == true){
   ...
    return;
}

if(...){
   ...
}
else{
   ...
}
// do some processing

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201447

No need to put anything in the if body, the else is skipped if the if is true. But, It would be cleaner to use a boolean negation like,

public void method1(){
    A a = new A();
    if (!a.method2()) {
        method3(); //<-- block not entered if method2() returns true.
    }
}

Upvotes: 1

Y.E.
Y.E.

Reputation: 922

You can use;

if(a.method2() == true){
    return;
}

But I really think you should question what you're trying to accomplish.

Upvotes: 0

Related Questions