Nested If else Condition with Java Program
“Nested If Else condition” is very popular in condition with java program. If we want to check more than one condition, then we can use “nested if else condition” in our java program. In this blog we will also introduce different between static and non-static method in main class.
Nested If Else Condition : Java Program
In this image, we can easily understand the nested if else condition. If condition1 is true then program’s flow goes to Cond-2 otherwise goes to Con-3. If Cond-2 is true then Statement – 1 executes otherwise Statement -2 executes.
If Condition1 is false then flow goes to Con-3. If Con-3 is true then Statement – 4 executes otherwise Statement – 3 executes. Using this images we can find maximum and minimum value of two numbers.
Static Method: Condition with java Program
Generally static keyword uses with static variable and method. Currently we will only focus on static method in main class. When we use static method in main class, we do not need any object for calling static method. So this is very important thing about static method. We will see use of static method in our video or static program.
Non Static Method : Condition with Java Program
If we use non-static method in program, so we have to create an object main class. In this example will introduce non-static method and calling process of this method using class object.
Program in “Nested If Else condition” with Static and Non-Static Method.
static void function1(int x) // Static method
System.out.println(“X is Zero”);
if(x>0) // Nested If else condition
System.out.println(“X is +ve”);
System.out.println(“X is -ve”);
System.out.println(“Non Static Method”);
public static void main(String args)
Condition2 obj = new Condition2(); // Object
obj.function2(); // call to non-static method with object
Scanner in1 = new Scanner(System.in);
System.out.print(“Enter X : ”);
x = in1.nextInt(); // Method
function1(x); // Calling to Static method without object
Enter X : 34
X is +ve
Video available on YouTube.
You can also download: Click Here -> Download Program
If you have problem on this blog, you can directly comment or subscribe on website.
Share and Enjoy
368total visits,1visits today