Video 31(ch7)

Yash
0

 



Program:-


public class MethodV31 {
    /*in the following method we use a static keyword which makes the method static and the static method is belongs to
    class and there is no need to make any object to call the method logic which is is static as the main method
     in which we call the operation of the method is also a static, and we know that only a static ca call the another
     static method without object creation we just have to assign a value to the parameters in the that static method and then print.*/
    static int logic(int x, int y){
        int z;
        if(x>y){
            z = x + y;
        }
        else{
            z = (x + y)*5;
        }
        return z;
    }
    int logic1(int u, int v){
        int w;
        if(u>v){
            w = u + v;
        }
        else{
            w = (u + v)*5;
        }
        return w;
    }
    //if we declare a method without using a static keyword there is a need of object creation an we do it as follows.
    public static void main(String[] args) {
        System.out.println("Without creating object:");
        int x = 5;
        int y = 7;
        int c;
        c = logic(x,y);
        System.out.println(c);
        System.out.println("By creating object:");
        MethodV31 obj = new MethodV31();
        System.out.println(obj.logic1(5,7));
    }
}

Post a Comment

0 Comments
Post a Comment (0)
To Top