KnowledgeBoat Logo

Computer Applications

When a method returns the value, the entire method call can be assigned to a variable. Do you agree with the statement?

User Defined Methods

60 Likes

Answer

Yes, when a method returns a value, we can assign the entire method call to a variable. The given example illustrates the same:


public class Example {

    public int sum(int a, int b) {
        int c = a + b;
        return c;
    }

    public static void main(String args[]) {
        Example obj = new Example();
        int x = 2, y = 3;
        int z = obj.sum(x, y);
        System.out.println(z);
    }
}

Answered By

38 Likes


Related Questions